reg = new RegExp("[\\u4E00-\\u9FFF]+", "g");
reg.test('中文') === true
// true
reg.test('中文') === false
// true
typeof(reg.test('中文'))
// "boolean"
reg = new RegExp("[\\u4E00-\\u9FFF]+", "g");
reg.test('中文') === true
// true
reg.test('中文') === false
// true
typeof(reg.test('中文'))
// "boolean"
5 回答10.1k 阅读✓ 已解决
9 回答2.6k 阅读✓ 已解决
14 回答3.8k 阅读
4 回答1.7k 阅读✓ 已解决
6 回答1.3k 阅读✓ 已解决
6 回答2k 阅读✓ 已解决
12 回答5.6k 阅读
使用 'g' 标志后,正则会进行多次匹配。每次执行
reg.test
,正则匹配的索引会发生变化。换句话说,执行reg.test
后返回的结果是会发生变化的。第一次执行reg.test
,匹配成功,返回 true,索引走到字符串最后一位;再次执行reg.test
,索引 + 1,匹配失败,返回 false。