[英]Regex for 2 words, non-numeric, and one space
I am trying to build a javascript regex that allows users to enter 2 words only, non-numeric, and only one space between them.我正在尝试构建一个 javascript 正则表达式,它允许用户仅输入 2 个单词,非数字,并且它们之间只有一个空格。 I have been trying this one: ^\w+\s*\w+?$ but don't know how to avoid numbers.
我一直在尝试这个: ^\w+\s*\w+?$ 但不知道如何避免数字。
You can try this:你可以试试这个:
([A-Za-z]+)\s([A-Za-z]+)$
If the 2 words only use the alphabets without any special characters.如果这两个词只使用没有任何特殊字符的字母。
You can use the simplest one.您可以使用最简单的一种。
/^([az]+)\s+([az]+)(\s*)$/gi
[a-z]+ tells any character in the alphabet with quantifier of one or more
\s* -> is a space with a quantifier of zero or more
i -> on the last part tells case-insensitive matching
let a = 'my Word'; let b = 'oneWord'; let c = 'using three words'; let d = 'onewordwithspace '; let e = 'my wordwithSpace '; let regEx = /^([az]+)\s+([az]+)(\s*)$/gi; console.log((regEx.test(a))); // true console.log((regEx.test(b))); // false console.log((regEx.test(c))); // false console.log((regEx.test(d))); // false console.log((regEx.test(e))); // true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.