欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript去掉空格的方法集合

 更新時間:2010年12月28日 21:41:59   作者:  
JavaScript去掉空格的方法集合,腳本之家以前發(fā)布過很多的去除空格的代碼,這個更多更全面。
實(shí)現(xiàn)1
復(fù)制代碼 代碼如下:

String.prototype.trim = function () {
return this .replace(/^\s\s*/, '' ).replace(/\s\s*$/, '' );
}

看起來不怎么樣,動用了兩次正則替換,實(shí)際速度非常驚人,主要得益于瀏覽器的內(nèi)部優(yōu)化。一個著名的例子字符串拼接,直接相加比用Array做成的StringBuffer還快。base2類庫使用這種實(shí)現(xiàn)。
實(shí)現(xiàn)2
復(fù)制代碼 代碼如下:

String.prototype.trim = function () {
return this .replace(/^\s+/, '' ).replace(/\s+$/, '' );
}

和實(shí)現(xiàn)1很相似,但稍慢一點(diǎn),主要原因是它最先是假設(shè)至少存在一個空白符。Prototype.js使用這種實(shí)現(xiàn),不過其名字為strip,因?yàn)镻rototype的方法都是力求與Ruby同名。
實(shí)現(xiàn)3
復(fù)制代碼 代碼如下:

String.prototype.trim = function () {
return this .substring(Math.max( this .search(/\S/), 0), this .search(/\S\s*$/) + 1);
}

以截取方式取得空白部分(當(dāng)然允許中間存在空白符),總共調(diào)用了四個原生方法。設(shè)計得非常巧妙,substring以兩個數(shù)字作為參數(shù)。Math.max以兩個數(shù)字作參數(shù),search則返回一個數(shù)字。速度比上面兩個慢一點(diǎn),但比下面大多數(shù)都快。
實(shí)現(xiàn)4
復(fù)制代碼 代碼如下:

String.prototype.trim = function () {
return this .replace(/^\s+|\s+$/g, '' );
}

這個可以稱得上實(shí)現(xiàn)2的簡化版,就是利用候選操作符連接兩個正則。但這樣做就失去了瀏覽器優(yōu)化的機(jī)會,比不上實(shí)現(xiàn)3。由于看來很優(yōu)雅,許多類庫都使用它,如JQuery與mootools
實(shí)現(xiàn)5
復(fù)制代碼 代碼如下:

String.prototype.trim = function () {
var str = this ;
str = str.match(/\S+(?:\s+\S+)*/);
return str ? str[0] : '' ;
}

match是返回一個數(shù)組,因此原字符串符合要求的部分就成為它的元素。為了防止字符串中間的空白符被排除,我們需要動用到非捕獲性分組(?:exp)。由于數(shù)組可能為空,我們在后面還要做進(jìn)一步的判定。好像瀏覽器在處理分組上比較無力,一個字慢。所以不要迷信正則,雖然它基本上是萬能的。
實(shí)現(xiàn)6
復(fù)制代碼 代碼如下:

String.prototype.trim = function () {
return this .replace(/^\s*(\S*(\s+\S+)*)\s*$/, '$1' );
}

把符合要求的部分提供出來,放到一個空字符串中。不過效率很差,尤其是在IE6中。
實(shí)現(xiàn)7
復(fù)制代碼 代碼如下:

String.prototype.trim = function () {
return this .replace(/^\s*(\S*(?:\s+\S+)*)\s*$/, '$1' );
}

和實(shí)現(xiàn)6很相似,但用了非捕獲分組進(jìn)行了優(yōu)點(diǎn),性能效之有一點(diǎn)點(diǎn)提升。
實(shí)現(xiàn)8
復(fù)制代碼 代碼如下:

String.prototype.trim = function () {
return this .replace(/^\s*((?:[\S\s]*\S)?)\s*$/, '$1' );
}

沿著上面兩個的思路進(jìn)行改進(jìn),動用了非捕獲分組與字符集合,用?頂替了*,效果非常驚人。尤其在IE6中,可以用瘋狂來形容這次性能的提升,直接秒殺火狐。
實(shí)現(xiàn)9
復(fù)制代碼 代碼如下:

String.prototype.trim = function () {
return this .replace(/^\s*([\S\s]*?)\s*$/, '$1' );
}

這次是用懶惰匹配頂替非捕獲分組,在火狐中得到改善,IE沒有上次那么瘋狂。
實(shí)現(xiàn)10
復(fù)制代碼 代碼如下:

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 : '' ;
}

我只想說,搞出這個的人已經(jīng)不是用牛來形容,已是神一樣的級別。它先是把可能的空白符全部列出來,在第一次遍歷中砍掉前面的空白,第二次砍掉后面的空白。全過程只用了indexOf與substring這個專門為處理字符串而生的原生方法,沒有使用到正則。速度快得驚人,估計直逼上內(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已經(jīng)告訴我們普通的原生字符串截取方法是遠(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)槟莻€whitespace 實(shí)現(xiàn)太長太難記了(當(dāng)然如果你在打造一個類庫,它絕對是首先)。實(shí)現(xiàn)11可謂其改進(jìn)版,前面部分的空白由正則替換負(fù)責(zé)砍掉,后面用原生方法處理,效果不遜于原版,但速度都是非常逆天。
實(shí)現(xiàn)12
復(fù)制代碼 代碼如下:

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);
}

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

復(fù)制代碼 代碼如下:

//String.prototype使用
//批量替換,比如:str.ReplaceAll([/a/g,/b/g,/c/g],["aaa","bbb","ccc"])
String.prototype.ReplaceAll=function (A,B) {
var C=this;
for(var i=0;i<A.length;i++) {
C=C.replace(A[i],B[i]);
};
return C;
};
// 去掉字符兩端的空白字符
String.prototype.Trim=function () {
return this.replace(/(^[\t\n\r]*)|([\t\n\r]*$)/g,'');
};
// 去掉字符左邊的空白字符
String.prototype.LTrim=function () {
return this.replace(/^[\t\n\r]/g,'');
};
// 去掉字符右邊的空白字符
String.prototype.RTrim=function () {
return this.replace(/[\t\n\r]*$/g,'');
};
// 返回字符的長度,一個中文算2個
String.prototype.ChineseLength=function()
{
return this.replace(/[^\x00-\xff]/g,"**").length;
};
// 判斷字符串是否以指定的字符串結(jié)束
String.prototype.EndsWith=function (A,B) {
var C=this.length;
var D=A.length;
if(D>C)return false;
if(B) {
var E=new RegExp(A+'$','i');
return E.test(this);
}else return (D==0||this.substr(C-D,D)==A);
};
// 判斷字符串是否以指定的字符串開始
String.prototype.StartsWith = function(str)
{
return this.substr(0, str.length) == str;
};
// 字符串從哪開始多長字符去掉
String.prototype.Remove=function (A,B) {
var s='';
if(A>0)s=this.substring(0,A);
if(A+B<this.length)s+=this.substring(A+B,this.length);
return s;
};

相關(guān)文章

最新評論