Search code examples
javascriptarraysregexprototypeiterable

matchAll Array for Group 1 Regex Matches


I'm trying to figure out a way to get all my Group 1 matches into an array without using a loop with matchAll().

Here's what I have thus far, but it only yields the first match:

let str = "123ABC, 123ABC"
let results = str.matchAll(/123(ABC)/gi);
let [group1] = results;
alert(group1[1]);

How can I get the results of the matchAll into one single array? Aka:

// ABC, ABC

Solution

  • You can use Array.from to convert results to array and perform map in one go:

    const matches = Array.from(results, match => match[1])