日期:2014-05-16 浏览次数:20541 次
String.prototype.trim = function() {
return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}String.prototype.trim = function() {
return this.replace(/^\s+/, '').replace(/\s+$/, '');
}String.prototype.trim = function() {
return this.substring(Math.max(this.search(/\S/), 0),this.search(/\S\s*$/) + 1);
}String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, '');
}String.prototype.trim = function() {
var str = this;
str = str.match(/\S+(?:\s+\S+)*/);
return str ? str[0] : '';
}String.prototype.trim = function() {
return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, '$1');
}
String.prototype.trim = function() {
return this.replace(/^\s*(\S*(?:\s+\S+)*)\s*$/, '$1');
}
String.prototype.trim = function() {
return this.replace(/^\s*((?:[\S\s]*\S)?)\s*$/, '$1');
}String.prototype.trim = function() {
return this.replace(/^\s*([\S\s]*?)\s*$/, '$1');
}
String.prototype.trim = function() {
var str = this,
whitespace = ' \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
for (var i = 0,len = str.length; i < len; i++) {
if (whitespace.indexOf(str.charAt(i)) === -1) {
str = str.substring(i);
break;
}
}
for (i = str.length - 1; i >= 0; i--) {
if (whitespace.indexOf(str.charAt(i)) === -1) {
str = str.substring(0, i + 1);
break;
}
}
return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}
String.prototype.trim = function() {
var str = this,
str = str.replace(/^\s+/, '');
for (var i = str.length - 1; i >= 0; i--) {
if (/\S/.test(str.charAt(i))) {
str = str.substring(0, i + 1);
break;
}
}
return str;
}
String.prototype.trim = function() {
var str = this,
str = str.replace(/^\s\s*/, ''),
ws = /\s/,
i = str.length;
while (ws.test(str.charAt(--i)));
return str.slice(0, i + 1);
}
实现 Firefox 2 IE 6
trim1 15ms < 0.5ms
trim2 31ms < 0.5ms
trim3 46ms 31ms
trim4 47ms 46ms
trim5