JS Regex Reference

Regex Syntax

/pattern/flagsLiteral regex
new RegExp('pattern', 'flags')Dynamic regex
^start / end$Anchors
. (any) \d (digit) \w (word) \s (space)Character classes
[abc] [^abc] [a-z]Character sets
? * + {n} {n,m}Quantifiers
(group) (?:non-capture) (?named)Groups
(?=ahead) (?!neg-ahead) (?<=behind)Lookaheads/behinds

Flags

g — globalFind all matches
i — case-insensitiveIgnore case
m — multiline^ and $ match line boundaries
s — dotAll. matches newlines
u — unicodeFull unicode support

Methods

str.match(re)Find matches
str.matchAll(re)Iterator of all matches (g flag)
str.replace(re, repl)Replace
str.replaceAll(re, repl)Replace all
re.test(str)Returns boolean
re.exec(str)Returns match array