December 2023
Intermediate to advanced
242 pages
5h 28m
English
The matchAll() method is similar to match() except when you set the global flag (g), which causes the method to provide additional information about the matches such as capturing groups and index positions:
| | const str = "9ft, 10ft, 11ft"; |
| | const re = /(\d\d)ft/g; |
| | |
| | console.log(str.match(re)); |
| | // → ["10ft", "11ft"] |
| | |
| | console.log(...str.matchAll(re)); |
| | // → ["10ft", "10", index: 5, input: "9ft, 10ft, 11ft", groups: undefined] |
| | // → ["11ft", "11", index: 11, input: "9ft, 10ft, 11ft", groups: undefined] |
| | |
| | console.log(re.exec(str)); |
| | // → ["10ft", "10", index: 5, input: "9ft, 10ft, 11ft", groups: undefined] |
matchAll() is a relatively new addition to ECMAScript compared to other methods ...
Read now
Unlock full access