正则表达式中文匹配

// 用正则匹配中文

var arr2 = ["匹","配","中","文"];
var pattern1 = /[\u4e00-\u9fa5]+/g;
for (var i = 0; i < arr2.length; i++) {
    if (pattern1.test(arr2[i])) {
        console.log(arr2[i]);    // 匹,中
    }
}

上面的代码为何只输出了 匹、中?

答案:

正则最后的g去掉

<script type="text/javascript">
    var arr2 = ["匹","配","中","文"];
    var pattern1 = /[\u4e00-\u9fa5]+/;
    for (var i = 0; i < arr2.length; i++) {
        if (pattern1.test(arr2[i])) {
            console.log(arr2[i]);    // 匹,中
        }
    }
</script>
posted @ 2017-11-06 16:28  Andy.Qiu  阅读(4907)  评论(0编辑  收藏  举报