Home > Web Front-end > JS Tutorial > How can you capture multiple groups in a JavaScript regexp when the repetition only keeps the last match?

How can you capture multiple groups in a JavaScript regexp when the repetition only keeps the last match?

DDD
Release: 2024-11-16 22:40:03
Original
710 people have browsed it

How can you capture multiple groups in a JavaScript regexp when the repetition only keeps the last match?

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+)+$/)
Copy after login

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"]
Copy after login

However, the regexp actually returns only the last captured match:

["foo bar baz", " baz"]
Copy after login

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:

  • Splitting on delimiters: If possible, consider splitting the string on the appropriate delimiter instead of using a regexp with repeated capturing groups.
  • Using a global exec loop: Match the repeated group in one match using the /pattern/g flag and then process the results using an exec loop. Note that this method is not entirely equivalent to a repeated capturing group.
  • Multilevel matching: Capture the repeated group in one match and then use a second regexp to break that match apart.

Example:

To match the string in a text using multilevel matching:

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(";"));
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template