Regex - Duplicate Capture Group

I am trying to figure out how I can repeat the capture group for values comma-separatedin the following line of URL:

id=1,2;name=user1,user2,user3;city=Oakland,San Francisco,Seattle;zip=94553,94523;

I use this one RegExpwhich returns the results that I want, except for the values, since they are dynamic, i.e. url parameter can be 2,3,4, etc. users, and I was wondering if I can create a capture group for each value instead of user1,user2,user3as a single capture group.

RegExp: (^|;|:)(\w+)=([^;]+)*

Here is a live demo of this online using RegExp

Output Example:

  • Group1 - (semicolon, colon)
  • Group2 - (key, i.e.Identifier, name, city, zip code)
  • Group3 - (value1)
  • Group 4 - (value 2) * if exists
  • Group 5 - (value 3) * if exists
  • 6 - ( 4) *,

.... , .

: ? * ?

+8
1

Regex , . , , . ( -expressions.info): /(abc|123)+/ 'abc123'. "abc", , "123". "123".

, , , . /(abc|123){2}/. "abc123" "123", "abc123abc". . , . , . , ( ).

, ";", "=", ",". [['id', '1', '2'], ['name', 'user1',...], ['city',...], ['zip',...]].

:

function (str) {
  var afterSplit = str.split(';|:');
  afterSplit.pop() // final semicolon creates empty string
  for (var i = 0; i < afterSplit.length; i++) {
    afterSplit[i] = afterSplit[i].split('=');
    afterSplit[i][1] = afterSplit[i][1].split(','); // optionally, you can flatten the array from here to get something nicer
  }
  return afterSplit;
}
+8

Source: https://habr.com/ru/post/1675005/


All Articles