Home >Web Front-end >JS Tutorial >How to Capture Multiple Groups in a Repeated JavaScript Regex?

How to Capture Multiple Groups in a Repeated JavaScript Regex?

Linda Hamilton
Linda HamiltonOriginal
2024-11-13 10:33:02684browse

How to Capture Multiple Groups in a Repeated JavaScript Regex?

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:

  • Split on Delimiters: If possible, use string splitting to separate the desired segments.
  • Exec Loop and Splitting: Use an exec loop to repeatedly match the pattern within a string and split the captured group into individual matches.
  • Multilevel Matching: Capture the repeated group in one match and then use another regex to extract the individual segments.

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

  • How do you access the matched groups in a JavaScript regex?

The above is the detailed content of How to Capture Multiple Groups in a Repeated JavaScript Regex?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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