Search code examples
javascriptregexregex-groupregex-negation

Regex match a dynamic pattern


Hey guys i got stuck with this problem:

I have this pattern:

(group1) ; (group2) ; (group3) ; ... Actually i don't know how many groups the rows can have and i need to match all groups between \s;\s.

I was thinking about a negative of pattern ^(!?\s;\s) but it didn't work


Solution

  • I would suggest doing a string split in this case, which also handles nicely the problem of not knowing how many groups you have, e.g.

    var input = "group 1 ; group 2 ; group 3";
    var groups = input.split(/\s*;\s*/);
    console.log(groups);