javascript Method for Removing Blank Lines from Regular Expressions

  • 2021-07-15 03:44:02
  • OfStack

Remove leading and subsequent spaces/(^\ s*) (\ s* $) /g examples are as follows:

Function body:


String.prototype.trim=function(){
return this.replace(/(^\s*)|(\s*$)/g,'');
}

Usage:


var s='  How do you do  ';
alert( s.trim() );

If you want to know all the spaces, including the middle ones, the example is as follows:


var s='a b c ';
String.prototype.clearSpacebar=function(){
return this.replace(/\s*/g,'');
}
alert( '|'+s.clearSpacebar()+'|');

Related articles: