I've been trying to create a regex that matches function parameters.
Here's the full sample (with debug mode): https://regex101.com/r/vM7xN1/1
The regex I currently have is this one: \(([^,\s\)]+)(?:\s*,\s*([^,\s\)]+))*\)
And the match results I'm trying to achieve:
1. someFunc(param) => ['param']
2. someFunc(param, param2) => ['param', 'param2']
3. someFunc(param, param2, param3) => ['param', 'param2', 'param3']
4. someFunc(param, param2, param3, param4) => ['param', 'param2', 'param3', 'param4']
For some reason, this matches only 1, 2 functions correcly. And in functions 3 and 4 it will only match the first param and the last param.
Why is it skipping the params between the first and the last?
Edit: Additional tests:
'myFunction(param1, param2, param3)'.match(/(([^,\s)]+)(?:\s*,\s*([^,\s)]+))*)/) => ["(param1, param2, param3)", "param1", "param3"]
When trying without the non-capturing group I get this:
'myFunction(param1, param2, param3)'.match(/(([^,\s)]+)(\s*,\s*([^,\s)]+))*)/) => ["(param1, param2, param3)", "param1", ", param3", "param3"]
Any help would be great.
Thanks!