JS中去除字符串空白符

海纳百川,有容乃大

1、通过原型创建字符串的trim()

//去除字符串两边的空白

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、函数实现

function trim(str){

return str.replace(/(^\s*)|(\s*$)/g, "");

}

 转自:https://www.cnblogs.com/mafeng/p/10249875.html

posted @ 2019-10-24 16:37  用脑袋行走的人  阅读(1044)  评论(0)    收藏  举报