String.prototype.matchAll()
The matchAll()
method returns an iterator of all results matching a string against a regular expression, including capturing groups.
Syntax
matchAll(regexp)
Parameters
regexp
: A regular expression object, or any object that has a
Symbol.matchAll
method.If
regexp
is not aRegExp
object and does not have aSymbol.matchAll
method, it is implicitly converted to aRegExp
by usingnew RegExp(regexp, 'g')
.If
regexp
is a regex, then it must have the global (g
) flag set, or aTypeError
is thrown.
Return value
An iterable iterator (which is not restartable) of matches. Each match is an array with the same shape as the return value of RegExp.prototype.exec()
.
Exceptions
TypeError
- : Thrown if the
regexp
is a regex that does not have the global (g
) flag set (itsflags
property does not contain"g"
).
- : Thrown if the
Description
The implementation of String.prototype.matchAll
itself is very simple — it simply calls the Symbol.matchAll
method of the argument with the string as the first parameter (apart from the extra input validation that the regex is global). The actual implementation comes from RegExp.prototype[@@matchAll]()
.