js 中文校验并过滤掉中文

js中文校验并过滤掉中文

CreateTime--2017年9月6日09:10:19

Author:Marydon

思路:

  1.判断字符串中是否包含中文;

  2.存在中文时,过滤掉。

举例:

var fileName = ",测试123";
// 过滤掉文件名中的中文(也包含日文和韩文),不包含中文符号
var regex = /[u4E00-u9FA5uF900-uFA2D]/;
// 包含中文
if (regex.test(fileName)) {
    // 用于临时存储单字符
    var chinese = "";
    // 用于校验是否是中文
    var flag = false;
    // 用于存储过滤后的文件名
    var filterChinese = "";
    for (var i=0; i < fileName.length; i++) {
        chinese = fileName.substring(i, i+1);
        flag = regex.test(chinese);
        // 该字符不是中文
        if(!flag) {
            filterChinese += chinese;
        }
    }
    // 过滤掉中文后的文件名
    fileName = filterChinese;
}
alert(fileName);// ,123

 相关推荐:

原文地址:https://www.cnblogs.com/Marydon20170307/p/7482905.html