All of them end up in RegExpBuiltinExec; they differ in what they return and who drives the loop.
| Need | Use | Returns |
|---|---|---|
| Does it match? | re.test(s) |
boolean (stateful under g/y) |
| First match with groups and index | s.match(re) without g, or re.exec(s) |
match array (index, input, groups) or null |
| All matched strings, nothing else | s.match(re) with g |
array of strings, or null |
| All matches with groups and index | s.matchAll(re) (g required) |
iterator of match arrays |
| Replace | s.replace(re, str | fn) |
new string; with a string pattern or no g, only the first match |
| Replace every match | s.replaceAll(re | str, str | fn) |
new string; a regex must have g |
| Split on a pattern | s.split(re) |
array; captured groups are spliced into the result |
execis the primitive. Withgoryit starts atlastIndexand updates it, so you drive the loop (while ((m = re.exec(s)))).matchAllis that loop done for you, on a clone of the regex, so your regex'slastIndexis untouched.replacecallbacks receive(match, ...captures, offset, string, groups);groupsis only passed when the pattern has named groups. In replacement strings,$1,$<name>,$&,$`,$'and$$are special.- Whether a string argument is a pattern depends on the method.
match,matchAllandsearchconvert a string to a regex.replace,replaceAll,split,includesandindexOftreat it literally. RegExp.escape(string)(ES2025) returns the string with every character that is special in a pattern escaped, sonew RegExp(RegExp.escape(input), 'g')matchesinputliterally. Node.js 24, Chrome 136, Firefox 134 and Safari 18.2 have it; older code useds.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').