<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <script>
        var result = (new RegExp("/^[\u4e00-\u9fa5]$/")).test("所");
        console.log(result);
    </script>
</head>
<body>
</body>
</html>
为什么输出的是false?文件用vim :set fileencoding 确认是用UTF-8编码的
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
var result = (new RegExp("^[\\u4e00-\\u9fa5]$")).test("所");
console.log(result);
跟 vim 没关系,本来结果就是
false,在 console 里打印一下new RegExp("/^[\u4e00-\u9fa5]$/")就知道了。或者
js里 // 是正则表达式字面量,new RegExp创建后面参数带的是正则的string,题主把概念搞混了
/[\u4E00-\u9FA5\uF900-\uFA2D]/.test(value);
其实上面好多不好使用!!试试这个
我也正好碰到这个问题了
/[\u4E00-\u9FA5\uF900-\uFA2D]/.test(value);也不ok呢