JavaScript String matchAll()
Syntax & Examples


String.matchAll() method

The matchAll() method of the String class in JavaScript returns an iterator of all the matches of a regular expression against a string. It provides a more comprehensive way to retrieve multiple matches along with capturing groups.


Syntax of String.matchAll()

The syntax of String.matchAll() method is:

matchAll(regexp)

This matchAll() method of String returns an iterator of all regexp's matches.

Parameters

ParameterOptional/RequiredDescription
regexprequiredA regular expression object. If a non-RegExp object is passed, it is implicitly converted to a RegExp.

Return Type

String.matchAll() returns value of type Iterator.



✐ Examples

1 Using matchAll() method to find all matches in a string

In this example, we use the matchAll() method to find all occurrences of the letter 'o' in a string.

For example,

  1. We define a string variable str with the value 'Hello World'.
  2. We call the matchAll() method on str with the regular expression /o/g to find all matches of the letter 'o'.
  3. The result, which is an iterator, is converted to an array using the Array.from() method and stored in the variable matches.
  4. We log matches to the console using console.log() method.

JavaScript Program

const str = 'Hello World';
const matches = Array.from(str.matchAll(/o/g));
console.log(matches);

Output

[['o'], ['o']]

2 Using matchAll() method with capturing groups

This example demonstrates using the matchAll() method to find all occurrences of a pattern with capturing groups.

For example,

  1. We define a string variable str with the value '2021-01-01 and 2022-12-31'.
  2. We call the matchAll() method on str with the regular expression /(\d{4})-(\d{2})-(\d{2})/g to find all date patterns and their capturing groups.
  3. The result, which is an iterator, is converted to an array using the Array.from() method and stored in the variable matches.
  4. We log matches to the console using console.log() method.

JavaScript Program

const str = '2021-01-01 and 2022-12-31';
const matches = Array.from(str.matchAll(/(\d{4})-(\d{2})-(\d{2})/g));
console.log(matches);

Output

[['2021-01-01', '2021', '01', '01'], ['2022-12-31', '2022', '12', '31']]

3 Using matchAll() method with a non-global regexp

In this example, we use the matchAll() method with a non-global regular expression, which results in a single match.

For example,

  1. We define a string variable str with the value 'foo bar baz'.
  2. We call the matchAll() method on str with the regular expression /ba./ to find the first occurrence of 'ba' followed by any character.
  3. The result, which is an iterator, is converted to an array using the Array.from() method and stored in the variable matches.
  4. We log matches to the console using console.log() method.

JavaScript Program

const str = 'foo bar baz';
const matches = Array.from(str.matchAll(/ba./));
console.log(matches);

Output

[['bar']]

Summary

In this JavaScript tutorial, we learned about matchAll() method of String: the syntax and few working examples with output and detailed explanation for each example.