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
以上是如何在重复的 JavaScript 正则表达式中捕获多个组?的详细内容。更多信息请关注PHP中文网其他相关文章!