js空格处理函数

原文:http://www.happyshow.org/content.asp?id=114

代码示例如下:

 1 <script language="javascript">
 2 String.prototype.trim=function(){
 3         return this.replace(/(^\s*)|(\s*$)/g, "");
 4 }
 5 String.prototype.ltrim=function(){
 6         return this.replace(/(^\s*)/g,"");
 7 }
 8 String.prototype.rtrim=function(){
 9         return this.replace(/(\s*$)/g,"");
10 }
11 
12 function test(){
13     var strWithSpace = '   12345   ';
14     window.alert(strWithSpace+"_"+strWithSpace.length);
15 
16     strWithSpace = strWithSpace.trim();
17     window.alert(strWithSpace+"_"+strWithSpace.length);
18 }
19 
20 window.onload = test ;
21 </script>
原文地址:https://www.cnblogs.com/ungshow/p/1282008.html