Home >Web Front-end >JS Tutorial >Summary of methods for filtering Chinese characters and spaces in JavaScript strings_javascript skills
How to filter Chinese characters or spaces in a string using js? There are many methods. We can use substitution and regular expressions to achieve this. This article introduces two simple examples to you. Interested friends can refer to them.
1.javascript filter spaces:
function moveSpace() { var str = " abc defg"; alert(str.replace(/[ ]/g, "")); } moveSpace();
2.javascript filter Chinese:
var title ="字符串zifuchuan" var reg=/[u4E00-u9FA5]/g; var result=title.replace(reg,''); alert(result);
3. JavaScript removes spaces at both ends of a string
String.prototype.trim=function (){return this.replace(/(^/s*)|(/s*$)/g,'');}
4.javascript removes spaces from strings
String.prototype.trim=function (){return this.replace(/(^/s*)|(/s*$)/g,'');}
The above is a summary of the methods for filtering Chinese characters and spaces in JavaScript strings introduced by the editor. I hope it will be helpful to everyone!