在 Javascript 字符串中检测俄语/西里尔字母?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26846663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detect Russian / cyrillic in Javascript string?
提问by Aerodynamika
I'm trying to detect if a string contains Russian (cyrillic) characters or not. I'm using this code:
我正在尝试检测字符串是否包含俄语(西里尔文)字符。我正在使用此代码:
term.match(/[\wа-я]+/ig);
but it doesn't work –?or in fact it just returns the string back as it is.
但它不起作用——或者实际上它只是按原样返回字符串。
Can somebody help with the right code?
有人可以帮忙提供正确的代码吗?
Thanks!
谢谢!
回答by Bohdan Lyzanets
Use pattern /[\u0400-\u04FF]/
to cover more cyrillic characters:
使用模式/[\u0400-\u04FF]/
覆盖更多西里尔字符:
// http://jrgraphix.net/r/Unicode/0400-04FF
const cyrillicPattern = /^[\u0400-\u04FF]+$/;
console.log('Прив?т:', cyrillicPattern.test('Прив?т'));
console.log('Hello:', cyrillicPattern.test('Hello'));
UPDATE:
更新:
In some new browsers, you can use Unicode property escapes.
在一些新的浏览器中,您可以使用Unicode 属性转义。
The Cyrillic script usesthe same range as described above: U+0400..U+04FF
Cyrillic 脚本使用与上述相同的范围:U+0400..U+04FF
const cyrillicPattern = /^\p{Script=Cyrillic}+$/u;
console.log('Прив?т:', cyrillicPattern.test('Прив?т'));
console.log('Hello:', cyrillicPattern.test('Hello'));
回答by Joey
Perhaps you meant to use the RegExp
test
method instead?
也许您打算改用该RegExp
test
方法?
/[а-яА-ЯЁё]/.test(term)
Note that JavaScript regexes are not really Unicode-aware, which means the i
flag will have no effect on anything that's not ASCII. Hence the need for spelling out lower- and upper-case ranges separately.
请注意,JavaScript 正则表达式并不真正识别 Unicode,这意味着该i
标志不会影响任何非 ASCII 的内容。因此需要分别拼写小写和大写范围。