Skip to main content
Version: 3.27.2

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 a RegExp object and does not have a Symbol.matchAll method, it is implicitly converted to a RegExp by using new RegExp(regexp, 'g').

      If regexp is a regex, then it must have the global (g) flag set, or a TypeError 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 (its flags property does not contain "g").

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]().