Regex Only单词和它们之间的单个空格



我不擅长编写正则表达式。要求是验证名称。

  • 只有字母应包含
  • 单词之间只有1个空格
  • 不能包含任何其他空格字符

使用

^[a-zA-Z]+(?: [a-zA-Z]+)*$

见证明。

解释

--------------------------------------------------------------------------------
^                        the beginning of the string
--------------------------------------------------------------------------------
[a-zA-Z]+                any character of: 'a' to 'z', 'A' to 'Z'
(1 or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
(?:                      group, but do not capture (0 or more times
(matching the most amount possible)):
--------------------------------------------------------------------------------
' '
--------------------------------------------------------------------------------
[a-zA-Z]+                any character of: 'a' to 'z', 'A' to 'Z'
(1 or more times (matching the most
amount possible))
--------------------------------------------------------------------------------
)*                       end of grouping
--------------------------------------------------------------------------------
$                        before an optional n, and the end of the
string

最新更新