JS: Regular Expression Functions

By Xah Lee. Date: . Last updated: .

Here is a overview of regex functions.

Do All Occurrences

str.matchAll(regex)

Return all occurrences and captures.

str.replaceAll(regex, rep)

Return a new string.

Do First (or All)

str.search(regex)

Return the index of first char of the match.

str.match(regex)
  • If regex has g flag → return all occurrences
  • If regex has no g flag → return captured groups.
  • If regex has no g flag nor capture → return first match.
str.replace(regex, rep)

Replace first occurrence (or all). Return a new string.

Return just boolean

regex.test(str)

Return true/false.

Get captured groups, or

regex.exec(str)

Return captured groups.

JavaScript. String

JavaScript. Regular Expression