Javascript 我想限制正则表达式中子域的数目
我想限制子域的级别仅为3级。尝试下面的正则表达式失败Javascript 我想限制正则表达式中子域的数目,javascript,regex,Javascript,Regex,我想限制子域的级别仅为3级。尝试下面的正则表达式失败 ([\.]?[a-z]*){3} 我的目标:abc.def.ghi 但是 上面的正则表达式接受abc.def.ghi。(注意最后一个)使用 ^(?[a-z]+(?:\[a-z]+){0,2})$ 看 解释 -------------------------------------------------------------------------------- ^ the beginn
([\.]?[a-z]*){3}
我的目标:abc.def.ghi
但是
上面的正则表达式接受abc.def.ghi。(注意最后一个
)使用
^(?[a-z]+(?:\[a-z]+){0,2})$
看
解释
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(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 (between 0 and
2 times (matching the most amount
possible)):
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
[a-z]+ any character of: 'a' to 'z' (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
){0,2} end of grouping
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
因为regexp没有锚定,所以它匹配字符串中的任何位置。非常感谢!9分钟后接受正确答案。