正则表达式中文匹配
// 用正则匹配中文
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>
原文链接:https://www.qiquanji.com/post/8442.html
本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。
微信扫码关注
更新实时通知