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 중국어 웹사이트의 기타 관련 기사를 참조하세요!