JS: String.prototype.matchAll

By Xah Lee. Date: . Last updated: .

New in JS2020.

str.matchAll(regex)
Returns a Iterator of all occurrences in str, or null if no match.
The regex must have the global flag g. [see RegExp Flags]
If arg is a string, it is converted to regex first by RegExp with added global flag g.

[see Regex Functions]

console.log(..."year 1999 and 2020".matchAll(/\d{4}/g));
// [ "1999" ] [ "2020" ]

If giving a RegExp Object as argument, it must have the global flag g

"year 1999".matchAll(/\d{4}/)
// error: Uncaught TypeError: String.prototype.matchAll called with a non-global RegExp argument
JS in Depth
XAH
Buy Xah JavaScript Tutorial
JS in Depth
XAH

JS Obj Ref

DOM


JS Obj Ref

String

prototype