在String的原型上实现去掉字符串空白字符。

String.prototype.space = function(){
return this.replace(/\s+/g, ""); //全部空格
}
String.prototype.leftSpace = function(){
return str.replace(/^\s*/g,""); //左空格
}
String.prototype.rightSpace = function(){
return str.replace(/\s*$/g,""); //右空格
}
String.prototype.lrSpace = function(){ //左右空格
return str.replace(/(^\s*)|(\s*$)/g, "");
}
var str = new String('     1     2    3      4     ');
console.log(str.space());
console.log(str.leftSpace());
console.log(str.rightSpace());
console.log(str.lrSpace());

posted @ 2019-02-11 17:33  小笨手  阅读(396)  评论(0编辑  收藏  举报