繁体   English   中英

对于逗号分隔

[英]For comma-separated

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

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

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

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

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

这些将是无效的标签:

hat, cat, rat, c3po, @gmail

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

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

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

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

在末尾引入空白:

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

从允许的字符中删除_

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

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

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

尝试这样的事情:

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个或多个单词字符。 ,? 匹配可选的逗号和空格。 (两个逗号将被拒绝。)周围的()+表示一次或多次。 最后,^和$将其锚定到字符串的开头和结尾,以确保所有内容均匹配。

假定下划线( _ )无效:

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

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

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

经过测试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM