Detect Russian / cyrillic in Javascript string?

2019-02-17 10:18发布

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!

2条回答
霸刀☆藐视天下
2楼-- · 2019-02-17 10:54

Use pattern /[\u0400-\u04FF]/ to cover more cyrillic characters:

// http://jrgraphix.net/r/Unicode/0400-04FF
const cyrillicPattern = /[\u0400-\u04FF]/;

console.log('Привіт:', cyrillicPattern.test('Привіт'));
console.log('Hello:', cyrillicPattern.test('Hello'));

查看更多
Summer. ? 凉城
3楼-- · 2019-02-17 11:09

Perhaps you meant to use the RegExp test method instead?

/[а-яА-ЯЁё]/.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.

查看更多
登录 后发表回答