gpt4 book ai didi

javascript - HTML5 模式仅允许 5 个逗号分隔的单词而不包含空格

转载 作者:行者123 更新时间:2023-11-28 13:36:46 25 4
gpt4 key购买 nike

我基本上不知道如何制作 HTML5 模式,所以这就是我问这个问题的原因。我只想创建一个模式,将其应用于 HTML5 新引入的 input[type=text] 彻底的 pattern 属性,但我不知道如何实现那种模式。

该模式包括以下内容:

  1. 仅允许 5 个逗号分隔的单词
  2. 无法添加空格。

最佳答案

^(\w+,){4}\w+$ 是您需要的模式:重复“任意数量的单词字符,后跟逗号”四次,然后“仅单词字符”。如果您想要“最多五个”,解决方案是

^(\w+,){0,4}\w+$

详细说明(改编自http://www.regex101.com):

^      assert position at start of the string (i.e. start matching from start of string)

1st Capturing group (\w+,){0,4}
Quantifier {0,4}: Between 0 and 4 times, as many times as possible, giving back as needed [greedy]

\w+ match any word character [a-zA-Z0-9_]
Quantifier +: Between one and unlimited times, as many times as possible, giving back as needed [greedy]

, matches the character , literally

\w+ match any word character [a-zA-Z0-9_]
Quantifier +: Between one and unlimited times, as many times as possible, giving back as needed [greedy]

$ assert position at end of the string

如果您不希望数字作为匹配的一部分,请将上面的每个 \w 替换为 [a-zA-Z] - 它将仅匹配小写和大写字母 A 到 Z。

更新针对您的评论,如果您不希望任何一组字符的长度超过10个,您可以将上面的表达式修改为

^(\w{1,10},){0,4}\w{1,10}$

现在“量词”是 {1,10} 而不是 + :这意味着“1 到 10 次之间”而不是“1 次或多次”。

关于javascript - HTML5 模式仅允许 5 个逗号分隔的单词而不包含空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20846919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com