js regular expression to remove spaces

//去掉左侧空格
"     武松打虎    2233     ".replace(new RegExp("^\\s*","g"),"")

//去掉右侧空格
"     武松打虎    2233     ".replace(new RegExp("\\s*$","g"),"")

//去掉所有空格
"      5555     6666    ".replace(new RegExp("\\s+","g"),"");  

Extended string functions


 /*去掉字符串两侧空格*/
String.prototype.myTrim = function()
{
    
    
  return   this.replace(new RegExp("^\\s*|\\s*$","g"),"");   
}

 /*去掉所有空格*/
String.prototype.myTrimAll = function()
{
    
     
   return this.replace(new RegExp("\\s+","g"),"");  
}

Reprint:
https://blog.csdn.net/liweibin_/article/details/8996016

Guess you like

Origin blog.csdn.net/u011511086/article/details/131783035