Saturday, December 13, 2025
HomeLanguagesJavascriptJavascript String matchAll() Method

Javascript String matchAll() Method

In Javascript, the matchAll() method is used to return all the iterators matching the reference string against a regex (regular expression). An important use of the matchAll() method is that it can be used to capture groups with the /g flag giving it an advantage over the match() method which ignores capturing groups with the /g flag.

Syntax:

str.matchAll(Regexp)

Parameter:

  • Regexp: It is simply a regular expression object. The RegExp object must include the /g flag, else an TypeError is thrown.

Return value: It is an iterator.

Example :

Javascript




function myFunction() { 
    
    //Regular expression with the /g flag
    const regex = /e(xam)(ple(\d?))/g;
    //Reference string
    const str = 'example1example2example3';
      
    //Using matchAll() method
    const array = [...str.matchAll(regex)];
      
    console.log(array[0]);
    console.log(array[1]);
    console.log(array[2]);
}  
myFunction();


Output: 

Javascript String matchAll() Method

Javascript String matchAll() Method

In the above example, we were able to find matches and also capture the internal groups as we used the matchAll() method. 

We have a complete list of Javascript Strings methods, to check those please go through Javascript String Complete reference article.

Supported Browser:

  • Chrome 1 and above
  • Edge 12 and above
  • Firefox 1 and above
  • Internet Explorer 4 and above
  • Opera 4 and above
  • Safari 1 and above
RELATED ARTICLES

Most Popular

Dominic
32446 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6814 POSTS0 COMMENTS
Nicole Veronica
11952 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12030 POSTS0 COMMENTS
Shaida Kate Naidoo
6949 POSTS0 COMMENTS
Ted Musemwa
7200 POSTS0 COMMENTS
Thapelo Manthata
6897 POSTS0 COMMENTS
Umr Jansen
6882 POSTS0 COMMENTS