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
Release: 2016-05-16 15:11:41
Original
1806 people have browsed it

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(); 
Copy after login

2.javascript filter Chinese:

var title ="字符串zifuchuan"
var reg=/[u4E00-u9FA5]/g;
var result=title.replace(reg,'');
alert(result); 
Copy after login

3. JavaScript removes spaces at both ends of a string

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

4.javascript removes spaces from strings

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

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template