最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JS check if string contains only cyrillic symbols and spaces - Stack Overflow

programmeradmin3浏览0评论

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!

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!

Share asked Sep 22, 2015 at 6:18 malcoaurimalcoauri 12.2k28 gold badges88 silver badges141 bronze badges 1
  • Try this link ? – huwence Commented Sep 22, 2015 at 6:29
Add a ment  | 

1 Answer 1

Reset to default 7

Try this:

var isValid = function(s) {
    return s && s.split(" ").length === 3 && /^[\u0400-\u04FF ]+$/.test(s);
};
发布评论

评论列表(0)

  1. 暂无评论