描述
该表达式将:
- 要求字符串以
-
字符开头
- 允许
#
在字符串中最多出现 1 次
- 防止空格出现在字符串中
- 允许一个或多个
a-z0-9
字符
- 允许
#
字符出现在字符串中的任何位置,包括开头和结尾
^(?!(?:.*?#){2,})-[a-z0-9#]+$
NODE EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
(?: group, but do not capture (at least 2
times (matching the most amount
possible)):
--------------------------------------------------------------------------------
.*? any character except \n (0 or more
times (matching the least amount
possible))
--------------------------------------------------------------------------------
# '#'
--------------------------------------------------------------------------------
){2,} end of grouping
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
- '-'
--------------------------------------------------------------------------------
[a-z0-9#]+ any character of: 'a' to 'z', '0' to '9',
'#' (1 or more times (matching the most
amount possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
例子
现场演示
样品
-abcdefghijklmnopqrstuvwxyz1234567890 = good
-abcdefghijklmnopq#rstuvwxyz1234567890 = good
-abcdefghijklmnopq##rstuvwxyz1234567890 = bad
-#abcdefghijklmnopqrstuvwxyz1234567890 = good
-##abcdefghijklmnopqrstuvwxyz1234567890 = bad
-#abcdefghijklmnopqrstuvwxyz1234567890 = good
-abcdefghijklmnopqrstuvwxyz1234567890# = good
-#abcdefghijklmnopqrstuvwxyz1234567890# = bad
#-abcdefghijklmnopqrstuvwxyz1234567890 = bad