簡體   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