JavaScript Filter string Chinese and whitespace method summary

  • 2021-01-22 04:50:00
  • OfStack

How does js filter Chinese characters or Spaces in a string? There are many methods, we can use substitution and regular expressions to achieve, this article to introduce you to two simple examples, interested friends can refer to 1.

1.javascript filter space:


function moveSpace() {
var str = " abc defg";
alert(str.replace(/[ ]/g, ""));
}
moveSpace(); 

2.javascript Filter Chinese:


var title =" string zifuchuan"
var reg=/[u4E00-u9FA5]/g;
var result=title.replace(reg,'');
alert(result); 

3. javascript removes Spaces at both ends of the string


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

4.javascript removes all Spaces from the string


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

The above is this site to introduce the JavaScript filter string in Chinese and space method summary, hope to help you!


Related articles: