<script type="text/javascript" language="javascript"> var s = " fOo bar "; for(var index = 0; index < s.length; index++){ alert(s.charCodeAt(index)); alert(s.charAt(index)); } //把左边的空格删除 function ltrim(s){ var flg = 0; var lstr = ''; while(s.charCodeAt(flg) == 32 )flg++; alert(flg); for(var index = 0; index < s.length - flg; index++) lstr+= s.charAt(flg+index); return lstr; } //把右边的空格删除 function rtrim(s){ var flg = 0; var rstr = ''; var strLength = s.length; while( s.charAt(strLength-1-flg) == ' ')flg++; for(var index = 0; index < s.length - flg; index++) rstr+= s.charAt(index); return rstr; } //利用正则表达式删除空格 function replaceSpace(str){ return str.replace(/\s/g,''); } //删除所有空格 function removeAllSpace(str){ var localString = ''; for(var index = 0; index<str.length; index++) if(str.charCodeAt(index)!= 32){ localString += str.charAt(index); }; return localString; } </script>
1 楼
lijiabin102
2008-11-21
利用正则表达式处理空格比较好用,我一般都用那个
2 楼
changqingonly
2008-11-24
lijiabin102 写道
利用正则表达式处理空格比较好用,我一般都用那个
你测试过他们的效率问题么?客户端处理,有必要对这些函数的时间要求么?
3 楼
我佛山人
2008-11-24
除了Minefield、Chrome和Opera,常用浏览器下是正则过滤的方法比你的快
4 楼
zhouyrt
2008-11-28
这样
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,"");//去除字符串两边的空白符
//return this.replace(/^\s+/g,"").replace(/\s$+/g,"");//去除字符串两边的空白符
//return this.replace(/^\s+/g,"");//去除字符串左边的空白符
//return this.replace(/\s$+/g,"");//去除字符串右边边的空白符
}
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,"");//去除字符串两边的空白符
//return this.replace(/^\s+/g,"").replace(/\s$+/g,"");//去除字符串两边的空白符
//return this.replace(/^\s+/g,"");//去除字符串左边的空白符
//return this.replace(/\s$+/g,"");//去除字符串右边边的空白符
}
5 楼
changqingonly
2008-11-28
zhouyrt 写道
这样String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,"");//去除字符串两边的空白符 //return this.replace(/^\s+/g,"").replace(/\s$+/g,"");//去除字符串两边的空白符 //return this.replace(/^\s+/g,"");//去除字符串左边的空白符 //return this.replace(/\s$+/g,"");//去除字符串右边边的空白符}
谢谢!