Home > Web Front-end > JS Tutorial > javascript 过滤字符串中的中文与空格

javascript 过滤字符串中的中文与空格

WBOY
Release: 2016-06-01 09:54:36
Original
1329 people have browsed it

1.javascript过滤空格:

<code class="language-javascript">function moveSpace() {
    var str = " abc defg";
    alert(str.replace(/[ ]/g, ""));
}
moveSpace();</code>
Copy after login

 

2.javascript过滤中文:

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

 

3. javascript去掉字符串两端的空格

<code class="language-javascript">String.prototype.trim=function (){return this.replace(/(^/s*)|(/s*$)/g,'');}</code>
Copy after login

 

4.javascript去掉字符串所以空格

<code class="language-javascript">String.prototype.sTrim = function (){return this.replace(//s/g, '');}</code>
Copy after login

 

Related labels:
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