<script> var str=" ab cd "; alert("["+str.trim()+"]"); </script> 去掉后面空格 <script> //实现去掉字符串的右空格 String.prototype.rtrim=function(){ return this.replace(/\s+$/ig,""); } ; //声明一个js字符串,方便测试去空格功能 var str…
1.正则去空格 a.去掉字符串中所有空格 " hello world ".replace(/\s+/g,"");//helloworld b.去掉字符串左边空格 var str = " hello world ".replace(/^\s*/g,"");//hello world.. c.去掉字符串右边空格 var str = " hello world ".replace(/\s*$/g,"&q…