【问题标题】:For comma-separated对于逗号分隔
【发布时间】:2010-11-30 17:45:23
【问题描述】:

我是 RegEx 和 JavaScript 的新手,我想知道是否有人知道 RegEx 用于检测输入字段是否包含以下类型的格式:

  • 至少一个可以包含空格的字母数字标签(例如“Test Tag”,但不能包含“Test@Tag”)

  • 每个标签用一个逗号分隔(例如“汽车、车辆、大型狗、床”,但不是“汽车、车辆、老虎)

我的意思是这样的一个例子,这些是 valid 标签:

boy, man,girl, woman,tyrannosaurus rex, lion

这些将是无效标签:

hat, cat, rat, c3po, @gmail

因为“@gmail”中有无效字符。

它也应该能够只接受一个标签,只要字符是字母数字。

【问题讨论】:

  • 您是在验证输入还是尝试从字符串中提取标签名称?如果您只是验证输入,它将是一个更简单的正则表达式。
  • 现在只是验证输入,提取部分稍后完成。
  • 你想在开头还是结尾允许额外的空格?
  • _ 应该是无效的,并且允许额外的空格,因为这些内容将在服务器端被过滤。

标签: javascript regex


【解决方案1】:

假设您要允许 _ 并且不允许开头或结尾的空格,这将是最短的解决方案:

/^\w(\s*,?\s*\w)*$/

在末尾引入空格:

/^\s*\w(\s*,?\s*\w)*\s*$/

从允许的字符中删除_

/^\s*[a-z0-9](\s*,?\s*[a-z0-9])*\s*$/

这是我最初发布的蛮力正则表达式。它将您的要求转换为正则表达式语法。我想把它留在这里以供参考。

/^\s*([a-z0-9]+(\s[a-z0-9]+)*)(\s*,\s*([a-z0-9]+(\s[a-z0-9]+)*))*\s*$/

【讨论】:

  • 还有一件事,如果你不介意的话,我怎么能在标签内只允许一个空格而没有更多呢?所以像这样的东西是无效的“Car, tyrannosaurus rex”但是“Car, Tyran nosaurus rex, Bike”是无效的?
  • @ThunderLegs 您收到的所有解决方案都没有考虑到这种情况,因此不可能更改其中的一个。
  • @ThunderLegs 我添加了另一个答案,其中包含一个单独的正则表达式,最多允许两个单词。
【解决方案2】:

试试这样的:

var re = /^(\w+,? ?)+$/;
var str1 = "boy, man,girl, woman,tyrannosaurus rex, lion";
var str2 = "hat, cat, rat, c3po, @gmail";
alert(str1.test(re));  // true
alert(str2.test(re));  // false

分解它... \w 匹配单词字符,\w+ 匹配 1 个或多个单词字符。 ,? ?匹配可选的逗号和空格。 (两个逗号将被拒绝。)所有内容周围的 ()+ 表示一次或多次。最后 ^ 和 $ 将其锚定到字符串的开头和结尾,以确保所有内容都匹配。

【讨论】:

  • bah,, 真,not ,, ok 真。太任性了。另外我不知道他是否要允许_
  • 不,这两个都评估为假。 ",?" 只允许使用 1 个逗号。你是正确的 \w 允许下划线字符。如果他想禁止,他可以使用 [a-zA-Z]。总体而言,他可能希望通过允许逗号后有多个空格或逗号前有多个空格来使其更宽松,但正则表达式按要求工作。
  • 谢谢,这很好用,是所有发布的正则表达式中最干净的。出于好奇,我将如何允许您描述的多个空间?
  • @James /^(\w+,? *)+$/ 将只允许在, 之后 之后使用空格,并且只允许空格,而不是任何空格。
  • @Alin - 是的,这是真的。我相信他就是这么问的。在“,?”之前再添加一个“*”如果你想在逗号之前允许空格。如果你不想允许尾随逗号,那么这样的事情会起作用: /^(\w+,? *)*\w+ *$/
【解决方案3】:

假设下划线 (_) 不无效:

/^(\w+\s?[\w\s]*)(,\s*\w+\s?[\w\s]*)*$/

Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
Match the regular expression below and capture its match into backreference number 1 «(\w+\s?[\w\s]*)»
   Match a single character that is a “word character” (letters, digits, and underscores) «\w+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
   Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s?»
      Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
   Match a single character present in the list below «[\w\s]*»
      Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
      A word character (letters, digits, and underscores) «\w»
      A whitespace character (spaces, tabs, and line breaks) «\s»
Match the regular expression below and capture its match into backreference number 2 «(,\s*\w+\s?[\w\s]*)*»
   Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
   Note: You repeated the capturing group itself.
   The group will capture only the last iteration.
   Put a capturing group around the repeated group to capture all iterations. «*»
   Match the character “,” literally «,»
   Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s*»
      Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
   Match a single character that is a “word character” (letters, digits, and underscores) «\w+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
   Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s?»
      Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
   Match a single character present in the list below «[\w\s]*»
      Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
      A word character (letters, digits, and underscores) «\w»
      A whitespace character (spaces, tabs, and line breaks) «\s»
Assert position at the end of a line (at the end of the string or before a line break character) «$»


Created with RegexBuddy

【讨论】:

    【解决方案4】:

    这里正在回答一个单独的问题。 如何做同样的事情但允许标签最多包含两个单词?

    /^\s*[a-z0-9]+(\s+[a-z0-9]+)?(\s*,\s*[a-z0-9]+(\s+[a-z0-9]+)?)*\s*$/
    

    经过测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-19
      • 1970-01-01
      • 1970-01-01
      • 2022-08-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多