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大比拼

posted @ 2013-08-17 19:58  eyotata  阅读(187)  评论(0编辑  收藏  举报