JavaScriptは、文字列内のスペースを取り除きます

文字列内のすべてのスペースを削除

関数トリム(STR){
     戻り str.replace(/ \ * S / G '' )。
} 
にconsole.log( '=' +トリム( 'Hello Worldの!')+ '=');   // = HelloWorldの!=

 

文字列にスペースを削除します

関数trimBothSides(STR){
     リターン str.replace(/ ^ \ sの* | \ sの*の$ / gで、 '' ); 
} 
にconsole.log( '=' + trimBothSides( 'Hello Worldの!')+ '=');  // =のHello World!=
console.log( '=' + 'のHello World!' .trim()+ '=');      // =のHello World!=

 

の左に空白の文字列を削除します

機能TRIMLEFT(文字列){
     リターン str.replace(/ ^ \ sの* /、 '' ); 
} 
にconsole.log( '=' + TRIMLEFT( '!のHello World')+ '=');     // =のHello World!=
console.log( '=' + 'のHello World!' .trimLeft()+ '=');      // =のHello World!=

 

文字列の右側に空白

関数trimRight(STR){
     リターン str.replace(/ \ ''、* $ / S ); 
} 
にconsole.log( '=' + trimRight( '!のHello World')+ '=');   // =のHello World!=
console.log( '=' + 'のHello World!' .trimRight()+ '=');     // =のHello World!=

 






















おすすめ

転載: www.cnblogs.com/yingtoumao/p/11520446.html