请帮助我为输入元素(<input pattern="myPattern">)的模式属性定义一个模式,它允许键入一个或多个按空格划分的哈希标签。例如:
#first //valid
#Second #and-3rd //valid
#one#two //invalid我尝试过(^|\s)(#[a-z\d-]+),但它只适用于输入中的一个标记。如何增强它以允许多个标记?
提前谢谢。
发布于 2016-11-12 13:51:21
您可以使用此正则表达式来允许以#开头并以空格分隔的单词:
^#[\w-]+(?:\s+#[\w-]+)*$RegEx描述:
^ # Start
# # match literal #
[\w-]+ # match 1 or more word chars or hyphen
(?: # start non-capturing group
\s+ # match 1 or more whitespace
# # match literal #
[\w-]+ # match 1 or more word chars or hyphen
)* # end of capturing group. * makes this group match 0 more times
$ # EndPS:注意到在input模式中使用reges时不需要锚定^和$。
https://stackoverflow.com/questions/40563119
复制相似问题