December 2023
Intermediate to advanced
242 pages
5h 28m
English
The match() method returns an array similar to exec(). You may have already noticed the similarity between these two methods. Consider this example:
| | const str = "About 100ft"; |
| | const re = /(\d+)ft/; |
| | |
| | console.log(str.match(re)); |
| | // → ["100ft", "100", index: 6, input: "About 100ft", groups: undefined] |
| | |
| | console.log(re.exec(str)); |
| | // → ["100ft", "100", index: 6, input: "About 100ft", groups: undefined] |
exec() and match() return the same result. The output differs only when you set the global flag (g):
| | const str = "9ft, 10ft, 11ft"; |
| | const re = /(\d\d)ft/g; |
| | |
| | console.log(str.match(re)); |
| | // → ["10ft", "11ft"] |
| | |
| | console.log(re.exec(str)); ... |
Read now
Unlock full access