日本免费全黄少妇一区二区三区-高清无码一区二区三区四区-欧美中文字幕日韩在线观看-国产福利诱惑在线网站-国产中文字幕一区在线-亚洲欧美精品日韩一区-久久国产精品国产精品国产-国产精久久久久久一区二区三区-欧美亚洲国产精品久久久久

ie 7/8不支持trim的屬性的解決方案( 二 )


}

這次是用懶惰匹配頂替非捕獲分組,在火狐中得到改善,IE沒有上次那么瘋狂 。
實(shí)現(xiàn)10
復(fù)制代碼 代碼如下:
String.prototype.trim = function () {
var str = this ,
whitespace = ’ ?u2000u2001u2002u2003u2004u20 05u2006u2007u2008u2009u200au200bu2028u2029 u3000’ ;
for ( var i = 0,len = str.length; 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 : ’’ ;
}

我只想說,搞出這個(gè)的人已不是用牛來形容,已是神一樣的級別 。它先是把可能的空白符全部列出來,在第一次遍歷中砍掉前邊的空白,第二次砍掉后面的空白 。全過程只用了indexOf與substring這個(gè)專門為處理字符串而生的原生方法,沒有施用到正則 。速度快得驚人,預(yù)計(jì)直逼上內(nèi)部的二進(jìn)制實(shí)現(xiàn),并且在IE與火狐(其它瀏覽器當(dāng)然也毫無疑問)都有杰出的表現(xiàn) 。速度都是零毫秒級另外 。
實(shí)現(xiàn)11
復(fù)制代碼 代碼如下:
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;
}

實(shí)現(xiàn)10已告訴咱們普通的原不認(rèn)識的字符串截取方法是遠(yuǎn)勝于正則替換,雖然是復(fù)雜一點(diǎn) 。但只要正則不過于復(fù)雜,咱們就可以利用瀏覽器對正則的優(yōu)化,改善程序執(zhí)行效率,從實(shí)現(xiàn)8在IE的表現(xiàn) 。我想通常不會有人在項(xiàng)目中應(yīng)用實(shí)現(xiàn)10,因?yàn)槟莻€(gè)whitespace 實(shí)現(xiàn)過長太難記了(當(dāng)然如果你在打造一個(gè)類庫,它絕對是起首) 。實(shí)現(xiàn)11可謂其改進(jìn)版,前邊部分的空白由正則替換負(fù)責(zé)砍掉,后面用原生方法處理,效果不遜于原版,但速度都是很是逆天 。
實(shí)現(xiàn)12
復(fù)制代碼 代碼如下:
String.prototype.trim = function () {
var str = this ,
str = str.replace(/^ss*/, ’’ ),
ws = /s/,
i = str.length;
while (ws.test(str.charAt(--i)));
return str.slice(0, i 1);
}

實(shí)現(xiàn)10與實(shí)現(xiàn)11在寫法上更好的改進(jìn)版,注意說的不是性能速度,而是易記與施用上 。和它的兩個(gè)先輩都是零毫秒級另外,以后就用這個(gè)來工作與嚇人 。
下面是老外給出的比力結(jié)果,執(zhí)行背景是對Magna Carta 這文章(超過27,600字符)進(jìn)行trim操作 。
實(shí)現(xiàn) Firefox 2 IE 6
trim1 15ms trim2 31ms trim3 46ms 31ms
trim4 47ms 46ms
trim5 156ms 1656ms
trim6 172ms 2406ms
trim7 172ms 1640ms
trim8 281ms trim9 125ms 78ms
trim10 trim11 trim12 trim函數(shù)實(shí)現(xiàn)揭曉自己的想法,想懂得原作者說什么請看原文 。


JS去除空格的方法目前共有12種:

實(shí)現(xiàn)1
String.prototype.trim = function() { return this.replace(/^ss*/, ’’).replace(/ss*$/, ’’); }
實(shí)現(xiàn)2
String.prototype.trim = function() { return this.replace(/^s /, ’’).replace(/s $/, ’’); }
實(shí)現(xiàn)3
String.prototype.trim = function() { return this.s string(Math.max(this.search(/S/), 0),this.search(/Ss*$/)1); }
實(shí)現(xiàn)4
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’); }
實(shí)現(xiàn)7
String.prototype.trim = function() { return this.replace(/^s*(S*(?:s S )*)s*$/, ’$1’); }
String.prototype.trim = function() { return this.replace(/^s*((?:[Ss]*S)?)s*$/, ’$1’); }
String.prototype.trim = function() { return this.replace(/^s*([Ss]*?)s*$/, ’$1’); }
String.prototype.trim = function() { var str = this, whitespace = ’ ??????????????? ’; for (var i = 0,len = str.length; ilen; i) { if (whitespace.indexOf(str.charAt(i)) === -1) { str = str.s string(i); break; } } for (i = str.length - 1; i = 0; i--) { if (whitespace.indexOf(str.charAt(i)) === -1) { str = str.s string(0, i1); break; } } return whitespace.indexOf(str.charAt(0)) === -1 ? str : ’’; }

推薦閱讀