去除前导和后续空格 /(^\s*)|(\s*$)/g 范例如下:
函数体:
String.prototype.trim=function(){
return this.replace(/(^\s*)|(\s*$)/g,'');
}
使用方法:
var s=' 你好 ';
alert( s.trim() );
如果要清楚所有空格,包括中间的,范例如下:
var s='a b c ';
String.prototype.clearSpacebar=function(){
return this.replace(/\s*/g,'');
}
alert( '|'+s.clearSpacebar()+'|');
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持本站!