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

时间:2021-06-10  来源:正则表达式  阅读:

去除前导和后续空格 /(^\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()+'|');

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

http://m.bbyears.com/aspjiaocheng/122975.html

推荐访问:
相关阅读 猜你喜欢
本类排行 本类最新