Detect Russian / cyrillic in Javascript string?
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'));
UPDATE:
In some new browsers, you can use Unicode property escapes.
The Cyrillic script uses the same range as described above: U+0400..U+04FF
const cyrillicPattern = /^\p{Script=Cyrillic}+$/u;
console.log('Привіт:', cyrillicPattern.test('Привіт'));
console.log('Hello:', cyrillicPattern.test('Hello'));
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.