Home > Article > Web Front-end > Why Does JavaScript Regex Capture Only the Last Group in a Repetition?
Capturing Multiple Groups in JavaScript Regular Expressions
When working with regular expressions in JavaScript, it's common to encounter situations where you need to capture multiple groups of data. However, in some cases, the expected behavior of capturing all matched groups may not be achieved.
For instance, consider the following JavaScript code:
"foo bar baz".match(/^(\s*\w+)+$/)
You might expect the code to return an array containing all of the captured matches:
["foo bar baz", "foo", " bar", " baz"]
However, the actual result is:
["foo bar baz", " baz"]
This occurs because in regular expressions, when you repeat a capturing group, only the last capture is retained. This is the default behavior in most regex flavors.
Workarounds for Capturing All Matches
If you need to capture all of the matched groups, there are a few options available:
Example Using an Exec Loop
The following example demonstrates matching multiple groups using an exec loop:
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) { console.log(match[1].split(";")); } // [ 'c', 'd', 'e', 'f' ] // [ 'xx', 'yy', 'zz' ]
Additional Resources
The above is the detailed content of Why Does JavaScript Regex Capture Only the Last Group in a Repetition?. For more information, please follow other related articles on the PHP Chinese website!