[javascript学习指南]javascript 正则表达式去空行方法

更新时间:2021-06-10    来源:正则表达式    手机版     字体:

【www.bbyears.com--正则表达式】

去除前导和后续空格 /(^\s*)|(\s*$)/g范例如下:

函数体:

 代码如下

String.prototype.trim=function(){

returnthis.replace(/(^\s*)|(\s*$)/g,'');

}

使用方法:

 代码如下

vars=' 你好 '

alert( s.trim() );

如果要清楚所有空格,包括中间的,范例如下:

 代码如下

vars='a b c '

String.prototype.clearSpacebar=function(){

returnthis.replace(/\s*/g,'');

}

alert('|'+s.clearSpacebar()+'|');

本文来源:http://www.bbyears.com/aspjiaocheng/122975.html

热门标签

更多>>

本类排行