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
Parameter | Optional/Required | Description |
---|---|---|
regexp | required | A 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,
- We define a string variable
str
with the value'Hello World'
. - We call the
matchAll()
method onstr
with the regular expression/o/g
to find all matches of the letter 'o'. - The result, which is an iterator, is converted to an array using the
Array.from()
method and stored in the variablematches
. - We log
matches
to the console usingconsole.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,
- We define a string variable
str
with the value'2021-01-01 and 2022-12-31'
. - We call the
matchAll()
method onstr
with the regular expression/(\d{4})-(\d{2})-(\d{2})/g
to find all date patterns and their capturing groups. - The result, which is an iterator, is converted to an array using the
Array.from()
method and stored in the variablematches
. - We log
matches
to the console usingconsole.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,
- We define a string variable
str
with the value'foo bar baz'
. - We call the
matchAll()
method onstr
with the regular expression/ba./
to find the first occurrence of 'ba' followed by any character. - The result, which is an iterator, is converted to an array using the
Array.from()
method and stored in the variablematches
. - We log
matches
to the console usingconsole.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.