Capturing Arbitrary Groups in JavaScript Regexp
When using capturing groups in JavaScript regular expressions, it's essential to understand the limitations. By default, only the last capture of a repeated group is kept, even if the group is nested.
Example: Nested Capture Group
Consider the following regular expression:
/^(\s*\w+)+$/
Expected output:
["foo bar baz", "foo", " bar", " baz"]
Actual output:
["foo bar baz", " baz"]
In this case, only the last captured group, " baz", is returned. This is because JavaScript only retains the final capture for each repeated group.
Options for Capturing Multiple Groups
To overcome this limitation, several options are available:
Example: Exec Loop and Splitting
Here's an example using an exec loop to capture and split a nested group:
var text = "a;b;<c;d;e;f>;g;h;i;<no no no>;j;k;<xx;yy;zz>"; var r = /<(\w+(;\w+)*)>/g; var match; while ((match = r.exec(text)) != null) { print(match[1].split(";")); } // Output: // ["c", "d", "e", "f"] // ["xx", "yy", "zz"]
In this example, the nested group is captured as group 1, which is then split on the semicolon delimiter.
Related Questions
The above is the detailed content of How to Capture Multiple Groups in a Repeated JavaScript Regex?. For more information, please follow other related articles on the PHP Chinese website!