Regex - Repeating Capturing Group
Regex doesn't support what you're trying to do. When the engine enters the capturing group a second time, it overwrites what it had captured the first time. Consider a simple example (thanks regular-expressions.info): /(abc|123)+/
used on 'abc123'
. It will match "abc" then see the plus and try again, matching the "123". The final capturing group in the output will be "123".
This happens no matter what pattern you try and any limitation you set simply changes when the regex will accept the string. Consider /(abc|123){2}/
. This accepts 'abc123' with the capturing group as "123" but not 'abc123abc'. Putting a capturing group inside another doesn't work either. When you create a capturing group, it's like creating a variable. It can only have one value and subsequent values overwrite the previous one. You'll never be able to have more capturing groups than you have parentheses pairs (you can definitely have fewer, though).
A possible fix then would be to split the string on ';', then each of those on '=', then the right-hand side of those on ','. That would get you [['id', '1', '2'], ['name', 'user1', ...], ['city', ...], ['zip', ...]]
.
That comes out to be:
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;
}
Capturing Group Repeated
String: !abc123def! regex: /!((abc|123|def)+)!/
Matchs:
Group 1: abc123def
Group 2: def
source: https://www.regular-expressions.info/captureall.html