Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 对于逗号分隔的_Javascript_Regex - Fatal编程技术网

Javascript 对于逗号分隔的

Javascript 对于逗号分隔的,javascript,regex,Javascript,Regex,我不熟悉正则表达式和JavaScript,我想知道是否有人知道正则表达式用于检测输入字段是否包含以下类型的格式: 至少一个可以包含空格的字母数字标记(例如,“测试标记”但不包含空格)Test@Tag)) 每个标签用一个逗号分隔(例如“汽车、车辆、大狗、床”,但不包括“汽车、车辆、老虎”) 我的意思的一个例子是,这些标签是有效的: boy, man,girl, woman,tyrannosaurus rex, lion hat, cat, rat, c3po, @gmail 这些标签将是无

我不熟悉正则表达式和JavaScript,我想知道是否有人知道正则表达式用于检测输入字段是否包含以下类型的格式:

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

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

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

boy, man,girl, woman,tyrannosaurus rex, lion
hat, cat, rat, c3po, @gmail
这些标签将是无效的:

boy, man,girl, woman,tyrannosaurus rex, lion
hat, cat, rat, c3po, @gmail
因为“@gmail”中有无效字符


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

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

/^\w(\s*,?\s*\w)*$/
在末尾引入空格:

/^\s*\w(\s*,?\s*\w)*\s*$/
正在从允许的字符中删除
\uuu

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

假设您希望允许
\u
并且在开头或结尾不允许空白,这将是最短的解决方案:

/^\w(\s*,?\s*\w)*$/
在末尾引入空格:

/^\s*\w(\s*,?\s*\w)*\s*$/
正在从允许的字符中删除
\uuu

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

试着这样做:

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

尝试以下操作:

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

假设下划线(
\u
)不是无效的:

/^(\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
假设下划线(
\uu
)不是无效的:

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

这里回答了一个单独的问题。如何做相同的事情,但允许标记最多包含两个单词


已测试。

此处将回答一个单独的问题。如何执行相同的操作,但允许标记最多包含两个单词



已测试。

您是在验证输入还是试图从字符串中提取标记名?如果只是验证输入,它将是一个简单得多的正则表达式。现在只是验证输入,提取部分将在稍后完成。是否要在开始或结束时允许额外的空格?\u应为无效且为ext允许使用ra空格,因为这些内容将在服务器端进行筛选。可能的重复项是您正在验证输入还是正在尝试从字符串中提取标记名?如果您只是验证输入,它将是一个简单得多的正则表达式。现在只验证输入,提取部分将在稍后完成。是否要在t处允许额外的空格开始还是结束?\应该是无效的,并且允许额外的空格,因为这些内容将被服务器端过滤。可能重复
bah,,
true,
not,,,ok
true。这太过宽容了。而且我不知道他是否希望允许
。不,这两个值都计算为false。只有1个逗号与“,?”,?“。您认为\w允许使用下划线字符是正确的。他可以使用[a-zA-Z]如果他想不允许。总的来说,他可能想通过在逗号后或逗号前允许多个空格来让它更自由,但是regexp可以按要求工作。谢谢,这很好,是所有regex中最干净的一个。出于好奇,我怎么能像你描述的那样允许多个空格?@James
/^(\w+,?*)+/
只允许在
之后使用空格,并且只允许空格,而不允许任何空格。@Alin-是的,这是真的。我相信他是在问这个问题。如果希望在逗号之前使用空格,请在“,?”之前再添加一个“*”。如果不希望在逗号后面使用空格,则类似的操作可以:/^(\w+,?*)*\w+*$/
bah,,
true,
not,,ok
true。这太放纵了。而且我不知道他是否想允许
\uuz
。不,这两个值都为false。只允许1个逗号与“,?”连用。您认为\w允许下划线字符是正确的。他可以使用[a-zA-Z]如果他想不允许。总的来说,他可能想通过在逗号后或逗号前允许多个空格来让它更自由,但是regexp可以按要求工作。谢谢,这很好,是所有regex中最干净的一个。出于好奇,我怎么能像你描述的那样允许多个空格?@James
/^(\w+,?*)+/
只允许在
之后使用空格,并且只允许空格,而不允许任何空格。@Alin-是的,这是真的。我相信他是在问这个问题。如果希望在逗号之前使用空格,请在“,?”之前再添加一个“*”。如果不希望在逗号后面使用空格,则类似的操作可以:/^(\w+,?*)*\w+*$/还有一件事,如果你不介意的话,我怎么能在标签内只允许一个空间,而不允许更多?所以像这样的东西是vaid“汽车,霸王龙”,但“汽车,霸王龙,自行车”是无效的吗?@ThunderLegs您收到的任何解决方案都没有考虑到这种情况,因此更改其中一个是不可能的。@ThunderLegs我添加了另一个答案,其中包含一个单独的正则表达式,最多允许两个单词。还有一件事,如果您不介意的话,我如何在标记中只允许一个空格而不允许更多?那么就像这是维德“车,霸王龙”b