以逗号分隔

时间:2010-11-30 17:45:24

标签: javascript regex

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

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

  • 每个标签用单个逗号分隔(例如“汽车,车辆,大型犬,床”,但不包括“汽车,车辆,虎”)

我的意思是这个例子,这些是有效标签:

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

这些无效标记:

hat, cat, rat, c3po, @gmail

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

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

4 个答案:

答案 0 :(得分:2)

假设你想允许_并且在开头或结尾不允许空白,这将是最短的解决方案:

/^\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*$/

答案 1 :(得分: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个或多个单词字符。 ,? ?匹配可选的逗号和空格。 (两个逗号将被拒绝。)()+围绕一切说一次或多次。最后^和$将它锚定到字符串的开头和结尾,以确保所有内容都匹配。

答案 2 :(得分:1)

假设下划线(_)无效:

/^(\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

答案 3 :(得分:1)

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

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

<强>测试。