Capturing Multiple Groups in JavaScript Regexp
When working with regular expressions in JavaScript, it is possible to capture multiple groups of matches within a string. However, there are limitations to the number of groups that can be captured and retrieved.
Consider the following JavaScript regexp:
"foo bar baz".match(/^(\s*\w+)+$/)
One might expect this regexp to return an array containing the entire string "foo bar baz" as well as each individual word:
["foo bar baz", "foo", " bar", " baz"]
However, the regexp actually returns only the last captured match:
["foo bar baz", " baz"]
This behavior occurs because, in most regexp flavors, only the last capture for a repeated capturing group is retained. In JavaScript, repeating a capturing group only allows for the capture of exactly the specified number of strings per match.
To overcome this limitation, there are several options:
Example:
To match the string
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(";")); }
This approach uses two levels of matching to extract individual words from the string.
The above is the detailed content of How can you capture multiple groups in a JavaScript regexp when the repetition only keeps the last match?. For more information, please follow other related articles on the PHP Chinese website!