16

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!

Aerodynamika
  • 7,883
  • 16
  • 78
  • 137
  • 2
    You include `\w` in the regular expression, so it matches words with Latin characters as well. – Pointy Nov 10 '14 at 15:06

2 Answers2

39

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'));
Bohdan Lyzanets
  • 1,652
  • 22
  • 25
22

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.

Joey
  • 344,408
  • 85
  • 689
  • 683
  • 3
    You might want to add `Ёё` since they are also used in Russian. – nhahtdh Nov 10 '14 at 15:35
  • the cyrillic unicode range doens't work, but the other method works great – Aerodynamika Nov 10 '14 at 16:17
  • This answers means you have to store your .js files as unicode. Hmm. – Cymro Apr 23 '19 at 19:01
  • @cymro, or use Unicode escape within the regex. But storing and transmitting text files as UTF-8 should really be the default nowadays. We're not in the 70s anymore. – Joey Apr 23 '19 at 22:07
  • Joey, thanks for your comment. Storing js files as UTF-8 often adds an unwanted BOM at the beginning. – Cymro May 08 '19 at 19:18