Home >Web Front-end >JS Tutorial >Summary of methods for filtering Chinese characters and spaces in JavaScript strings_javascript skills

Summary of methods for filtering Chinese characters and spaces in JavaScript strings_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:11:411806browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn