Regex Cheat Sheet

A complete JavaScript regular expression reference. All syntax elements with descriptions and examples — bookmark this page for quick lookups while writing regex patterns.

Open the Regex Tester

Character Classes

Character classes match a single character from a defined set.

SyntaxDescription
.Any character except newline (use s flag to include newlines)
\dAny digit — equivalent to [0-9]
\DAny non-digit character
\wWord character — [a-zA-Z0-9_]
\WNon-word character
\sAny whitespace — space, tab, newline
\SAny non-whitespace character
[abc]Character set — matches a, b, or c
[^abc]Negated set — matches anything except a, b, or c
[a-z]Character range — matches any letter a through z

Quantifiers

Quantifiers specify how many times the preceding element must occur. By default they are greedy (match as many characters as possible). Add ? to make them lazy.

SyntaxDescription
*0 or more (greedy)
+1 or more (greedy)
?0 or 1 — makes the preceding element optional
{n}Exactly n times
{n,}n or more times
{n,m}Between n and m times (inclusive)
*?0 or more (lazy — matches as few as possible)
+?1 or more (lazy)
??0 or 1 (lazy)

Anchors

Anchors match a position in the string rather than a character.

SyntaxDescription
^Start of string. With m flag: start of each line
$End of string. With m flag: end of each line
\bWord boundary — position between a word char and non-word char
\BNon-word boundary

Groups & Alternation

Groups capture portions of the match for extraction or backreference. Use them to apply quantifiers to multi-character sequences.

SyntaxDescription
(abc)Capture group — captured in match[1], match[2], etc.
(?:abc)Non-capturing group — groups without capturing
(?<name>abc)Named capture group — accessible as match.groups.name
\1, \2Backreference to capture group 1, 2… (in pattern)
$1, $2Backreference in replace string (use $& for full match)
a|bAlternation — matches a or b

Lookahead & Lookbehind

Lookaround assertions match a position based on what precedes or follows, without consuming characters.

SyntaxDescription
(?=abc)Positive lookahead — matches if followed by abc
(?!abc)Negative lookahead — matches if NOT followed by abc
(?<=abc)Positive lookbehind — matches if preceded by abc
(?<!abc)Negative lookbehind — matches if NOT preceded by abc

Flags

Flags modify how the regex engine interprets the pattern. In JavaScript, flags are passed as the second argument to new RegExp() or appended after the closing / delimiter.

SyntaxDescription
gGlobal — find all matches, not just the first
iCase-insensitive — treat uppercase and lowercase as equal
mMultiline — ^ and $ match start/end of each line
sDotAll — makes . match newlines too
uUnicode — full Unicode support, enables \p{} escapes
ySticky — matches only at lastIndex position
dIndices — includes start/end indices of each match group

Special Characters & Escaping

These characters have special meaning in regex. Prefix them with \ to match them literally.

SyntaxDescription
\.Literal dot (unescaped . matches any char)
\*Literal asterisk
\nNewline
\tTab character
\rCarriage return
\uXXXXUnicode code point (4 hex digits)

Ready to test a pattern? Open the live regex tester — real-time matching, group highlighting, and a patterns library.

Open Regex Tester