欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

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

程序员文章站 2024-01-08 18:43:40
...
1.javascript过滤空格:
function moveSpace() {
    var str = " abc defg";
    alert(str.replace(/[ ]/g, ""));
}
moveSpace();

2.javascript过滤中文:

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

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

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

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

String.prototype.sTrim = function (){return this.replace(//s/g, '');}