I am trying to parse the following string format:
<id>:<name>[,<name>]*
As an example, consider 123:test,south-west,best,rest_well
.
I wrote the following regex:
/(\d+):([a-zA-Z0-9_\-]+)(?:,([a-zA-Z0-9_\-]+))*/
My assumption was that (?:,([a-zA-Z0-9_\-]+))
would capture optional additional names (south-west, best, and rest_well). However, it only captures the last name 'rest_well'.
The printed out match:
'123:test,south-west,best,rest_well'.match(/(\d+):([a-zA-Z0-9_\-]+)(?:,([a-zA-Z0-9_\-]+))*/);
> ["123:test,south-west,best,rest_well", "123", "test", "rest_well"]
What I was expecting:
> ["123:test,south-west,best,rest_well", "123", "test", "south-west", "best", "rest_well"]
I believe other languages would actually accumulate the matched groups but somehow this fails. Maybe I am missing a small detail. Any help is appreciated!