用于特定名称验证的Javascript正则表达式
我有一个大学作业要写一个JS正则表达式来验证名称:名称可以在开头和结尾包含空格(不要问任何问题,这是我们老师的要求),也可以包含这样的字符:用于特定名称验证的Javascript正则表达式,javascript,regex,Javascript,Regex,我有一个大学作业要写一个JS正则表达式来验证名称:名称可以在开头和结尾包含空格(不要问任何问题,这是我们老师的要求),也可以包含这样的字符:-,'和空格(“”)。目前,我的正则表达式如下所示: var Nameregex = /^( ?)*[A-Z]+((['-])?[a-z]+)*(([ ]?[a-z]*)*)*$/g; 它的工作原理几乎完美,但只有一种情况例外:一个单词(单词之间用空格和-符号隔开)只能包含一个'符号 例如,像John-andrew'andrew'John这样的名字不应该起
-
,'
和空格(“”)。目前,我的正则表达式如下所示:
var Nameregex = /^( ?)*[A-Z]+((['-])?[a-z]+)*(([ ]?[a-z]*)*)*$/g;
它的工作原理几乎完美,但只有一种情况例外:一个单词(单词之间用空格和-
符号隔开)只能包含一个'
符号
例如,像John-andrew'andrew'John
这样的名字不应该起作用。但是John-andrew'John-andrew'John
应该有效添加一个(?!.'[a-Za-z]+')
在^
之后的消极前瞻:
/^(?。*'[A-Za-z]+'\s*[A-z]+(?:['-]?[A-z]+)*(?:\s*[A-z]*)*$/
看
解释
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
.* any character except \n (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 look-ahead
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
[A-Z]+ any character of: '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)):
--------------------------------------------------------------------------------
['-]? any character of: ''', '-' (optional
(matching the most amount possible))
--------------------------------------------------------------------------------
[a-z]+ any character of: 'a' to 'z' (1 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
)* end of grouping
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more times
(matching the most amount possible)):
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
[a-z]* any character of: 'a' to 'z' (0 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
)* end of grouping
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
谢谢它几乎可以完美地工作。忘了提一下,像约翰·安德鲁这样的名字也应该有用。因此,最后的正则表达式看起来像:
/^(?)*[A-Za-z]+)*[A-z]+(?:['-]?[A-z]+)*(?:\s[A-z]*)*$/
在第三个字符集中添加了空格字符。再次感谢您的帮助:)