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

原文地址:https://www.cnblogs.com/planetwithpig/p/11733048.html