js去除空格多个方法
1、prototype方式去除空格
复制代码 代码示例:
String.prototype.Trim = function()
{
return this.replace(/(^s*)|(s*$)/g, "");
}
String.prototype.LTrim = function()
{
return this.replace(/(^s*)/g, "");
}
String.prototype.Rtrim = function()
{
return this.replace(/(s*$)/g, "");
}
2、全文匹配替换
复制代码 代码示例:
var regExp = new RegExp("需要被替换的字符',"g")
var text = "…………";
text = text.replace(regExp , "替换的字符");
3、正则匹配替换
复制代码 代码示例:
String .prototype.trim = function(){
var matches = this.match(/^[ tnr]+/);
var prefixLength = (matches == null) ? 0:matches[0].length;
matches = this.match(/[ trn]+$/);
var suffixLength = (matches == null) ? 0:matches[0].length;
return this.slice(prefixLength,this.length-suffixLength);
}
例子:
复制代码 代码示例:
function strTrim(str){
return str.replace(/(^s*)|(s*$)/g,"");
}