JS: String.prototype.match
String argument
str.match(findStr)-
The string findStr is converted to regex regexObj by regex constructor RegExp, then
str.match(regexObj)is called.
Regex argument
str.match(regexObj)-
Return a array, or null if no match.
If regexObj has Regex Flag g (global), the result is all occurences.
Else, the result is array, first element is the first matched string, second element and rest is captured groups (if any), also has property keys
index,input,groups. Result is the same asregexObj.exec(str). see JS: RegExp.prototype.exec
🟢 tip: if you need to get all captured groups of all occurrences, use String.prototype.matchAll .
Example. get all occurrences
const xtext = "year 2026-10-24 3201-08-17"; const xresult = xtext.match(/\d\d\d\d-(\d\d)-(\d\d)/g); console.log(xresult); // [ "2026-10-24", "3201-08-17" ]
Example. get first match and all captures
/* get first match and all captures */ const xtext = "year 2026-10-24 3201-08-17"; const xresult = xtext.match(/\d\d\d\d-(\d\d)-(\d\d)/); console.log(xresult); /* [ "2026-10-24", "10", "24", index: 5, input: "year 2026-10-24 3201-08-17", groups: undefined ] */ /* first match is "2026-10-24" capture 1 is the month "10" capture 2 is the date "24" */
RegExp.prototype[Symbol.match] property
🛑 warning: String.prototype.match internally calls RegExp.prototype[Symbol.match].
It means, you can customize String.prototype.match by adding a symbol property Symbol.match function to the regex object.