matchAll() JavaScript String Method – Explained with Examples
Whenever you use matchAll() on a string, the method does the following:
- It creates an iterator object.
- It populates the newly created iterator with all the patterns that match the method’s regular expression argument.
Syntax of the matchAll()
Method
matchAll()
accepts a regular expression argument. Here is the syntax:
Examples
Below are examples of the matchAll()
method.
Use matchAll()
to match a pattern that contains no capturing group
Let’s discuss converting the iterator in the snippet above to an array.
How to convert an iterator to an array object
The three typical ways to convert an iterator to an array object are by using:
Array.from()
for...of
- Spread operator (
...
)
Let’s discuss the three techniques.
How to use Array.from()
to convert an iterator to an array object
In the returned array above,
'day'
refers to the matched pattern.index
refers to the index position where the matched pattern began.input
indicates the string on which you invokedmatchAll()
.groups
contains a collection of the named capturing groups you specified inmatchAll()
’sRegExp
argument. Orundefined
if the regular expression does not contain any named capturing group.
Let’s now see how to use the for...of
loop to convert an iterator to an array object.
How to use for...of
to convert an iterator to an array object
In the returned array above,
'day'
refers to the matched pattern.index
refers to the index position where the matched pattern began.input
indicates the string on which you invokedmatchAll()
.groups
contains a collection of the named capturing groups you specified inmatchAll()
’sRegExp
argument. Orundefined
if the regular expression does not contain any named capturing group.
Let’s now see how to use the spread operator to convert an iterator to an array object.
How to use the spread operator to convert an iterator to an array object
In the returned array above,
'day'
refers to the matched pattern.index
refers to the index position where the matched pattern began.input
indicates the string on which you invokedmatchAll()
.groups
contains a collection of the named capturing groups you specified inmatchAll()
’sRegExp
argument. Orundefined
if the regular expression does not contain any named capturing group.
Let’s now see another example of the matchAll()
method.
Use matchAll()
to match a pattern containing a capturing group
In the returned array above,
'day'
refers to the matched pattern.'a'
indicates the matched capturing group pattern.index
refers to the index position where the matched pattern began.input
indicates the string on which you invokedmatchAll()
.groups
contains a collection of the named capturing groups you specified inmatchAll()
’sRegExp
argument. Orundefined
if the regular expression does not contain any named capturing group.
Let’s now see another example of the matchAll()
method.
Use matchAll()
to match a pattern containing two capturing groups
In the returned array above,
'Sunday'
refers to the matched pattern.'Su'
indicates the first matched capturing group pattern.'a'
indicates the second matched capturing group pattern.index
refers to the index position where the matched pattern began.input
indicates the string on which you invokedmatchAll()
.groups
contains a collection of the named capturing groups you specified inmatchAll()
’sRegExp
argument. Orundefined
if the regular expression does not contain any named capturing group.
Let’s now see another example of the matchAll()
method.
Use matchAll()
to match a pattern containing three capturing groups
In the returned arrays above,
'sun'
and'Sunday'
refers to the matched pattern.'su'
,'Su'
,'day'
, and'a'
indicates the matched capturing group patterns.index
refers to the index position where the matched pattern began.input
indicates the string on which you invokedmatchAll()
.groups
contains a collection of the named capturing groups you specified inmatchAll()
’sRegExp
argument. Orundefined
if the regular expression does not have any named capturing group.- The two
undefined
values beforeindex: 7
indicate that the second and third capturing groups are not defined in the first matched pattern:'sun'
.
Design in Figma, launch in Webflow
Let’s now see another example of the matchAll()
method.
Use matchAll()
to match a pattern containing a named capturing group
In the returned array above,
'day'
refers to the matched pattern.'a'
indicates the matched capturing group pattern.index
refers to the index position where the matched pattern began.input
indicates the string on which you invokedmatchAll()
.groups
contains a collection of the named capturing groups you specified inmatchAll()
’sRegExp
argument. Orundefined
if the regular expression does not contain any named capturing group.
Let’s now see another example of the matchAll()
method.
Use matchAll()
to match a pattern containing two named capturing groups
In the returned array above,
'Sunday'
refers to the matched pattern.'Su'
indicates the first matched capturing group pattern.'a'
indicates the second matched capturing group pattern.index
refers to the index position where the matched pattern began.input
indicates the string on which you invokedmatchAll()
.groups
contains a collection of the named capturing groups you specified inmatchAll()
’sRegExp
argument. Orundefined
if the regular expression does not contain any named capturing group.
Now, let’s discuss the differences between matchAll()
and match()
.
matchAll()
vs. match()
– What’s the Difference?
The three main differences between matchAll()
and match()
are these:
1. Returned value
matchAll()
returns an iterator while match()
returns an array.
Here’s an example:
2. Global flag
matchAll()
does not work without a global flag.
However, match()
works irrespective of the presence or absence of a global flag.
Here’s an example:
Learn CSS Grid with Images
3. Capturing group
matchAll()
works better with capturing groups than the match()
method.
For instance, if you use a global flag (g
) with match()
’s RegExp argument, the computer will ignore the regular expression’s capture groups.
Example
However, matchAll()
allows easy access to capture groups.
Here’s an example: