JS: String.prototype.matchAll
(new in JS: ECMAScript 2020)
str.matchAll(findStr)
-
If argument findStr is a string, it is converted to regex regexObj by RegExp with added RegExp Flag global. Then
str.matchAll(regexObj)
is called. str.matchAll(regexObj)
-
Return a Iterable Object (is also an Iterator) of all occurrences and captures, or return null if no match.
The argument regexObj must have the RegExp Flag global, else it's error.
For each item in result, it is an array with meaning like this:
[matchStr, capture1, capture2, etc]
The first element of each array is occurrence of match. Rest elements are the captures. (Captures are always substring of the occurrence.)
const xx = "year 2010, 2015, and 2020"; const yy = xx.matchAll(/\d\d(\d)(\d)/g); for (let x of yy) console.log(x); /* [ "2010", "1", "0", index: 5, input: "year 2010, 2015, and 2020", groups: undefined ] [ "2015", "1", "5", index: 11, input: "year 2010, 2015, and 2020", groups: undefined ] [ "2020", "2", "0", index: 21, input: "year 2010, 2015, and 2020", groups: undefined ] */
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
Verifying Result is Iterable and Iterator
/* check matchAll result is iterable and iterator */ const xx = "year 1999 and 2020".matchAll(/\d/g); console.log( Reflect.has(xx, Symbol.iterator), Reflect.has(xx, "next"), );
JavaScript. Regular Expression
- JS: RegExp Tutorial
- JS: Regex Functions
- JS: Create Regex Object
- JS: RegExp Syntax
- JS: RegExp Flag
- JS: Regex Replace String Dollar Sign
- JS: Regex Replace Function Args
- JS: RegExp (class)
- JS: RegExp Constructor
- JS: RegExp.prototype
- JS: String.prototype.search
- JS: String.prototype.match
- JS: String.prototype.matchAll
- JS: String.prototype.replace
- JS: String.prototype.replaceAll
- JS: RegExp.prototype.test
- JS: RegExp.prototype.exec
JS String.prototype
- JS: String.prototype.constructor
- JS: String.prototype.at
- JS: String.fromCharCode
- JS: String.prototype.concat
- JS: String.prototype.repeat
- JS: String.prototype.trim
- JS: String.prototype.trimStart
- JS: String.prototype.trimEnd
- JS: String.prototype.padStart
- JS: String.prototype.padEnd
- JS: String.prototype.slice
- JS: String.prototype.substring
- JS: String.prototype.substr
- JS: String.prototype.indexOf
- JS: String.prototype.lastIndexOf
- JS: String.prototype.includes
- JS: String.prototype.startsWith
- JS: String.prototype.endsWith
- JS: String.prototype.search
- JS: String.prototype.match
- JS: String.prototype.matchAll
- JS: String.prototype.replace
- JS: String.prototype.split
- JS: String.prototype.toLowerCase
- JS: String.prototype.charAt
- JS: String.prototype.charCodeAt
- JS: String.prototype.codePointAt