Regex Course
Cheatsheet
A quick reference — the essential syntax of modern JavaScript regex on a single page. Press Ctrl/Cmd + P to print it.
Regex · Cheatsheet — eLearner.app
Basic syntax
Literal patterns
/hello/ // matches "hello" /Hello/i // case-insensitive /^abc/ // anchored to start of stringSlashes delimit the pattern; letters match themselves.
Meta-characters
. ^ $ * + ? ( ) [ ] { } | \To match them literally escape them with \.
Wildcard .
/a.c/ // a + any character + c /a.c/s // . also matches \n (s flag)
Predefined classes
Character classes
\d digit [0-9] \D non-digit \w word [A-Za-z0-9_] \W non-word \s whitespace \S non-whitespaceCustom classes
[abc] one of a, b, c [a-z] range a..z [A-Za-z0-9_] same as \w [^abc] NEGATED: anything except a, b, c [.,;:] literal punctuationUnicode (with u flag)
\p{L} any Letter \p{N} Number \p{S} Symbol \p{Script=Latin} Latin alphabet \P{L} non-letter
Quantifiers
Base
a* 0 or more a a+ 1 or more a a? 0 or 1 a (optional) a{3} exactly 3 a{2,5} from 2 to 5 a{2,} at least 2Greedy vs lazy
.+ greedy: as much as possible .+? lazy: as little as possible .*? lazy zero-or-more a{2,5}? lazy versionLazy adds ? after the quantifier.
Anchors
Boundaries
^ start of string (or line with m flag) $ end of string (or line with m flag) \b word boundary \B NON-word boundary \A \z absolute start/end (not in JS)Examples
/^abc/ string starting with abc /abc$/ string ending with abc /\bword\b/ exact word /^\s*$/m empty line (with m flag)
Groups and alternatives
Groups
(abc) capturing, accessible as $1 (?:abc) non-capturing (?<name>abc) named: groups.name / $<name> a|b|c alternatives (a or b or c)Backreferences
(\w+)\s+\1 consecutive duplicate word (?<a>\w)\k<a> named backreference String.replace(/(\w+) (\w+)/, '$2 $1')
Lookaround (zero-width)
Lookahead
(?=...) positive: must be followed by ... (?!...) negative: must NOT be followed by ... \d+(?= USD) digits only if followed by " USD"Lookbehind (ES2018+)
(?<=...) positive: must be preceded by ... (?<!...) negative: must NOT be preceded by ... (?<=\$)\d+ digits only if preceded by $
Flags (canonical order: dgimsuvy)
The 8 flags
g global (all matches) i ignore case m multiline (^ and $ per line) s dot all (. matches \n) u Unicode (enables \p{...}) v Unicode v-mode (set operations) y sticky (from lastIndex only) d has indicesTypical combinations
gi "find all, case-insensitive" gm lines in multiline text gs dot-all blocks across multiple lines gu real texts with accents / emojis
Replace and split
Basic replace
'foo bar'.replace(/o/g, '0') // 'f00 bar' 'John Doe'.replace(/(\w+) (\w+)/, '$2 $1') // '2024-03-15' -> '15/03/2024' date.replace(/(\d{4})-(\d{2})-(\d{2})/, '$3/$2/$1');Callback replace
str.replace(/\d+/g, (m) => Number(m) * 2); str.replace(/(\d{4})-(\d{2})/, (_, y, m) => `$\{m\}/$\{y\}`);The callback receives match, groups, offset, original, groups.
Split with regex
'a, b ,c, d'.split(/\s*,\s*/) // ['a','b','c','d'] 'a=1; b=2'.split(/\s*;\s*/) // ['a=1', 'b=2']Replace references
$1 $2 ... numbered group $<name> named group $& full match $` text before match $' text after match $$ literal $
Useful patterns
Email (general-purpose)
/[\w.+-]+@[\w-]+(?:\.[\w-]+)+/To validate for real: send confirmation email.
Permissive IPv4
/\b(?:\d{1,3}\.){3}\d{1,3}\b/URL http(s)
/https?:\/\/[\w.-]+(?::\d+)?(?:\/[^\s]*)?/Decimal number
/-?\d+(?:\.\d+)?/Trim whitespace
s.replace(/^\s+|\s+$/g, '')
Anti ReDoS
Catastrophic patterns
(a+)+b NESTED -> explodes (a|a)* overlapping ALTERNATIVES (\w+\s+)+ greedy repeated groupRefactoring
^(\w+\s+)+$ dangerous ^(?:\w+\s+)*\w+$ safe .*?,.*?,.*?, limit backtracking with lazyDisjoint your choices, avoid nesting, validate input length.