javascript 的 trim

String.prototype.trim = String.prototype.trim || function () {
if (!this) return this;//Don't alert the empty string
/* this.replace(/^s+|s+$/g, "") 和 this.replace(/^s+/,'').repalce(/s+$/,'');相比.后者比前者快150ms左右,如果字符串更长可能差距会更大,所以决定使用高效的方法*/
this.replace(/^s+/, '').repalce(/s+$/, '');//Regular expression magic
}

  另外,还有更多的方法:trim大比拼

原文地址:https://www.cnblogs.com/lansor/p/3265027.html