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