COMPLETE REFERENCE - JAVASCRIPT
Regex Cheatsheet
Every regular expression token in one place, each with a plain-English explanation - character classes, anchors, quantifiers, groups, lookarounds, flags and search-and-replace. Want to test a pattern? Open the regex tester.
CHARACTERS
. Any character except a line break
a.c -> "abc", "a c"
\. A literal dot (escape the metacharacter)
3\.14 -> "3.14"
\t Tab
\n Line break (newline)
\r Carriage return
\uffff Character by hex code point
CHARACTER CLASSES
[abc] One character: a, b or c
[^abc] One character that is NOT a, b or c
[a-z] One character in the range a to z
[a-zA-Z0-9] Combine ranges - any letter or digit
\d A digit
[0-9]
\D A non-digit
\w A word character: letter, digit or underscore
\W A non-word character
\s Whitespace: space, tab or line break
\S Non-whitespace
[a-fA-F0-9] A hex digit (no shorthand - spell out the range)
\p{L} A Unicode letter (needs the u flag); \p{N} any number
ANCHORS & BOUNDARIES
^ Start of string (or line with the m flag)
$ End of string (or line with the m flag)
\b Word boundary
\bcat\b
\B Not a word boundary
QUANTIFIERS
* Zero or more of the preceding element
+ One or more
? Zero or one (optional)
{n} Exactly n times
{n,} n or more times
{n,m} Between n and m times
*? +? {n,m}? Lazy: match as few as possible
".*?"
GROUPS & ALTERNATION
(...) Capture group (numbered \1, \2 ...)
(?:...) Non-capturing group
(?<name>...) Named capture group; reference with \k<name>
| Alternation (this OR that)
cat|dog
\1 Backreference to capture group 1
(\w+) \1
LOOKAROUNDS
(?=...) Positive lookahead - followed by
\d+(?=px)
(?!...) Negative lookahead - NOT followed by
(?<=...) Positive lookbehind - preceded by
(?<=\$)\d+
(?<!...) Negative lookbehind - NOT preceded by
FLAGS
g Global - find all matches
i Ignore case
m Multiline - ^ and $ match at every line break
s Dotall - . also matches line breaks
u Unicode mode (enables \p{...})
y Sticky - match only from lastIndex
SEARCH & REPLACE
str.replace(re, "...") Replace matches (use the g flag for all)
$1 $2 Insert capture group 1, 2 in the replacement
$<name> Insert a named capture group
$& Insert the whole match
FROM CHEATSHEET TO ACTUALLY KNOWING IT
A cheatsheet helps you recognize tokens. Writing patterns from memory is a different skill. The free course drills every construct on this page with live, checked exercises - from literal characters to lookarounds.