ArticleZip > How To Return Only Captured Groups In A Javascript Regex With Multiple Matches

How To Return Only Captured Groups In A Javascript Regex With Multiple Matches

When working with regular expressions in JavaScript, capturing groups are incredibly useful. They allow you to extract specific parts of a matched string. However, when dealing with multiple matches, you may want to focus solely on the captured groups rather than the full match. In this article, we will discuss how you can achieve this by returning only the captured groups in a JavaScript regex with multiple matches.

To begin, let's consider a scenario where you have a regex pattern with multiple capturing groups and several matches within a string. You want to extract and return just the captured groups, excluding the complete match itself.

One way to accomplish this is by using the `matchAll()` method available for strings in JavaScript. This method returns an iterator containing all the matches, including captured groups. You can then iterate over this iterator and extract only the captured groups.

Here is an example to demonstrate this technique:

Javascript

const text = "Hello, World! How are you?";
const regex = /b(w+)b/g;

const matches = [...text.matchAll(regex)];
const capturedGroups = matches.map(match => match.slice(1));

console.log(capturedGroups);

In this example, we first define a sample text and a regex pattern that captures words. We then use the `matchAll()` method to obtain all matches, including the captured groups. By converting the iterator to an array and mapping over each match to extract the captured groups (using `slice(1)` to exclude the full match), we can effectively return only the captured groups from each match.

Another approach to achieve this is by using the `exec()` method in a loop to iterate through each match and access the captured groups individually. The `exec()` method returns an array containing the full match as the first element, followed by the captured groups.

Here is how you can implement this technique:

Javascript

const text = "12345, 67890";
const regex = /(d)(d)(d)/g;

let match;
const capturedGroups = [];

while ((match = regex.exec(text)) !== null) {
  capturedGroups.push(match.slice(1));
}

console.log(capturedGroups);

In this example, we define a text string containing numbers and a regex pattern that captures digits in groups of three. By using a `while` loop in conjunction with the `exec()` method, we iterate through each match and extract the captured groups into an array.

By employing these methods, you can effectively return only the captured groups in a JavaScript regex with multiple matches. Whether you choose to use `matchAll()` or `exec()` in a loop, the key is to access the captured groups within each match and handle them accordingly based on your requirements.

Experiment with these techniques in your own projects to enhance your regex capabilities and efficiently work with multiple matches and captured groups in JavaScript.

×