JS: String.prototype.matchAll
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 flagg
. [see RegExp Flags]
If arg is a string, it is converted to regex first by RegExp with added global flagg
.
[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