Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS check if string contains only cyrillic symbols and spaces

Tags:

javascript

I have some string and I need to check if this string:

a) consists of 3 words b) contains ONLY cyrillic symbols and spaces

My code:

var isValid;
isValid = function(s) {
  return s && s.split(" ").length === 3 && /[а-яА-Я ]/.test(s);
};

But this code doesn't work, because isValid('a b c') returns 'true'. What is my mistake? Thanks in advance!

like image 795
malcoauri Avatar asked Nov 01 '25 15:11

malcoauri


1 Answers

Try this:

var isValid = function(s) {
    return s && s.split(" ").length === 3 && /^[\u0400-\u04FF ]+$/.test(s);
};
like image 134
Ty Kroll Avatar answered Nov 04 '25 07:11

Ty Kroll



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!