最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regex for multiple words separated by spaces or commas - Stack Overflow

programmeradmin4浏览0评论

I'm trying to create regex for multiple strings separated by ma or space.

Lorem Ipsum // valid
Lorem, Ipsum //valid
Lorem, Ipsum, Ipsum, Ipsum // multiple valid
Lorem // invalid without space/ma

Here is what i have so far:

^\w+(,\s*\w+){3}$/

I'm trying to create regex for multiple strings separated by ma or space.

Lorem Ipsum // valid
Lorem, Ipsum //valid
Lorem, Ipsum, Ipsum, Ipsum // multiple valid
Lorem // invalid without space/ma

Here is what i have so far:

^\w+(,\s*\w+){3}$/
Share Improve this question edited Dec 15, 2017 at 6:59 Alan Moore 75.2k13 gold badges107 silver badges160 bronze badges asked Dec 14, 2017 at 19:21 CamilaCamila 1771 gold badge2 silver badges14 bronze badges 6
  • So they must have a space but optionally a ma? Or are ma separated values (with no space) also valid such as Lorem,Ipsum? Also, is Lorem ,Ipsum valid? On that note, are Lorem, , Ipsum, Lorem,, Ipsum or Lorem Ipsum valid? Basically, what are your exact requirements/conditions? – ctwheels Commented Dec 14, 2017 at 19:23
  • Single words are accepted. If there is anything else after word example "Test" space or ma than it should be not accepted – Camila Commented Dec 14, 2017 at 19:37
  • I'm sorry, it's not clear what you're looking for. – ctwheels Commented Dec 14, 2017 at 19:42
  • 1 Are you looking for \w+(?:[, ]+\w+)+? – ctwheels Commented Dec 14, 2017 at 19:50
  • Try ^\w+(?:(?:,\s\w+)+|(?:\s\w+)+)$ - it will match a string that starts with 1+ word chars, and then has 1 or more repetitions of , followed with a whitespace and 1+ word chars or 1+ repetitions of a space followed with 1+ word chars. Another way of writing it is ^\w+(?=(,?\s))(?:\1\w+)+$ – Wiktor Stribiżew Commented Dec 15, 2017 at 7:58
 |  Show 1 more ment

4 Answers 4

Reset to default 9

You may use

^\w+(?:(?:,\s\w+)+|(?:\s\w+)+)$

See the regex demo.

The regex matches:

  • ^ - start of string
  • \w+ - 1+ word chars
  • (?: - start of an alternation group:
    • (?:,\s\w+)+ - ,, whitespace, 1+ word chars
    • | - or
    • (?:\s\w+)+ - whitespace and then 1+ word chars
  • ) - end of group
  • $ - end of string.

You may shorten the pattern using a lookahead and a capturing group:

^\w+(?=(,?\s))(?:\1\w+)+$

See the regex demo. Here, the difference is (?=(,?\s))(?:\1\w+)+:

  • (?=(,?\s)) - a positive lookahead that checks if there is an optional , and then a whitespace immediately to the right of the current location and captures that sequence into Group 1
  • (?:\1\w+)+ - 1 or more sequences of:
    • \1 - the same text captured into Group 1
    • \w+ - 1+ word chars.

See the regex demo.

Try this regex:

^(\w+[, ]+)*\w+$

Assuming that you want to match the whole phrase:

^(\w+(,|\s)\s*)+\w+$

should do the trick.

Since none of the above worked for me, I came up with this:

/([^,\s]+)/g
发布评论

评论列表(0)

  1. 暂无评论