这是lgzx的问题,问的
方法添加到JS字符串,
删除的空白字符的字符串两边(包括空格,制表符,进纸等)。
复制代码代码如下所示:
string.prototype.trim =
函数(){
/ /返回this.replace((^ +)/( + $)} / g,); / /中间空白字符串被删除
/ /返回this.replace( / ^ + | + $ /克); / /
返回this.replace( / ^ + / g,)。更换( / S + $ / g,);
}
jquery1.4.2,MooTools使用
复制代码代码如下所示:
功能trim1(STR){
返回(str.replace / ^( 的| xA0)+ |(的| xA0 + $ / g),);
}
jquery1.4.3,原型的使用,这种方式去除G更好的
性能在一个小规模的字符串
处理性能更好
复制代码代码如下所示:
功能trim2(STR){
返回(+ / str.replace / ^(的| u00a0),更换('')/(的| u00a0)+ $ /,);
}
进行性能测试后,Steven Levithan提出了最快的切割线JS,它具有更好的性能,在处理长字符串。
复制代码代码如下所示:
功能trim3(STR){
STR = str.replace((的| u00a0) / / ^ +,' ');
对于(var i = str.length-1;我> = 0;我--){
如果( / /。试验(str.charat(我))){
STR = str.substring(0,i + 1);
打破;
}
}
返回字符;
}
最后要提到的是当地的修剪方法(15.5.4.20)加到在ECMA-262字符串(V5)。此外,该
发动机还增加了molliza Gecko 1.9.1 trimleft,trimright方法字符串。