skip to Main Content

I am trying to use a regex to match a list of elements separated by a comma in JavaScript.
The regex I use is the following: ([a-z]+)(?:,([a-z]+))*, but I am not able to get all the matched groups, only the first and the last.

I made a regex101 to illustrate my problem: https://regex101.com/r/lIkE8k/5.

Is there something I’m missing?

2

Answers


  1. Given your sample bold,italic,underlign,blink and what you appear to want, this regex would work

    s*([a-z]+)(?=s*,|s*$)  
    

    https://regex101.com/r/b3exXD/1

    Login or Signup to reply.
  2. This simple regex gives you all capture groups when using matchAll.

    const csv="example,using,several words,and phrases";
    const matches = csv.matchAll(/([^,]+)/g);
    for (const match of matches) {
        console.log(match[0]);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search