Home  >  Article  >  Web Front-end  >  Why Does JavaScript Regex Capture Only the Last Group in a Repetition?

Why Does JavaScript Regex Capture Only the Last Group in a Repetition?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-11 04:12:02219browse

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:

  • Split on delimiters: If the pattern you're trying to match contains delimiters, consider using the split method instead of a regular expression. This approach can be used to split the input string into an array of strings.
  • Matching using a loop: You can use a loop to repeatedly apply the regular expression to the input string using the exec method. This method returns an array containing all of the captured groups for each match.
  • Multilevel matching: Another approach involves capturing the repeated group in one match and then using a second regular expression to break that match apart.

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

  • regular-expressions.info/Repeating a Capturing Group vs Capturing a Repeating Group
  • How do you access the matched groups in a javascript regex?

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!

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