Regular expression for a list of items separated by comma or by comma and a space

后端 未结 8 1167
天涯浪人
天涯浪人 2020-12-15 05:02

Hey, I can\'t figure out how to write a regular expression for my website, I would like to let the user input a list of items (tags) separated by comma or by comma and a spa

相关标签:
8条回答
  • 2020-12-15 05:12

    something like this should work: ((apple|pie|applepie),\s?)*

    0 讨论(0)
  • 2020-12-15 05:13

    I often work with coma separated pattern, and for me, this works :

    ((^|[,])pattern)+
    

    where "pattern" is the single element regexp

    0 讨论(0)
  • 2020-12-15 05:18

    Here's a simpler solution:

        console.log("test, , test".match(/[^,(?! )]+/g));

    It doesn't break on empty properties and strips spaces before and after properties.

    0 讨论(0)
  • 2020-12-15 05:29
    ([^,]*)
    

    Look For Commas within a given string, followed by separating these. in regards to the whitespace? cant you just use commas? remove whitespace?

    0 讨论(0)
  • 2020-12-15 05:30

    This might work:

    ([^,]*)(, ?([^,]*))*
    
    0 讨论(0)
  • 2020-12-15 05:32

    Assuming the words in your list may be letters from a to z and you allow, but do not require, a space after the comma separators, your reg exp would be [a-z]+(,\s*[a-z]+)*

    This is match "ab" or "ab, de", but not "ab ,dc"

    0 讨论(0)
提交回复
热议问题