简体   繁体   English

JavaScript正则表达式可匹配2个单词和一个长度受限制的空白字符

[英]JavaScript Regex to match 2 words and an whitespace character with length limitations

I actually got this regex ^[A-zÀ-ÿ ]{3,50}$ or ^[A-zÀ-ÿ\\s]{3,50}$ that finds 3 to 50 characters of this specific alphabets. 我实际上得到了此正则表达式^[A-zÀ-ÿ ]{3,50}$^[A-zÀ-ÿ\\s]{3,50}$ ,可以找到3至50个此特定字母的字符。 I need a new regex to accept only 1 whitespace character \\s maintaining the {3,50} limitation. 我需要一个新的正则表达式只接受1空白字符\\s保持{3,50}限制。

I tried ^[A-zÀ-ÿ]+\\s[A-zÀ-ÿ]{3,50}$ but it is limiting the last tuple to 3-50 and not the whole thing. 我试过了^[A-zÀ-ÿ]+\\s[A-zÀ-ÿ]{3,50}$但这将最后一个元组限制为3-50,而不是整个对象。

Any help would be appreciated, Thank you 任何帮助,将不胜感激,谢谢

Actually, to match ASCII letters, you need to use [A-Za-z] , not [Az] (see this SO thread ). 实际上,要匹配ASCII字母,您需要使用[A-Za-z]而不是[Az] (请参见此SO线程 )。

As for the single obligatory whitespace, it can be added as in your attempt, and the length limitation can be added in the form of a lookahead: 对于单个必填空格,可以按照您的尝试添加它,并且可以以超前形式添加长度限制:

/^(?=.{3,50}$)[A-Za-zÀ-ÿ]+\s[A-Za-zÀ-ÿ]+$/
  ^^^^^^^^^^^^ 

See the regex demo . 参见regex演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM