Skip to content

Reference

Regex Cheat Sheet

Quick reference for JavaScript regular expression syntax. Test patterns live with the Regex Tester.

Character Classes

PatternDescriptionExample
.Any character except newline (unless s flag is set)a.c → abc, a1c, a-c
\dAny digit [0-9]\d{3} → 123, 456
\DAny non-digit [^0-9]\D+ → abc, ---
\wAny word character [A-Za-z0-9_]\w+ → hello, var_1
\WAny non-word character [^A-Za-z0-9_]\W → @, #, !
\sAny whitespace (space, tab, newline)\s+ → matches spaces
\SAny non-whitespace character\S+ → hello
[abc]Character set — matches a, b, or c[aeiou] → vowels
[^abc]Negated set — any character except a, b, c[^0-9] → non-digits
[a-z]Character range — a through z[A-Za-z] → letters

Quantifiers

PatternDescriptionExample
*Zero or more of the preceding elementab*c → ac, abc, abbc
+One or more of the preceding elementab+c → abc, abbc
?Zero or one of the preceding element (optional)colou?r → color, colour
{n}Exactly n of the preceding element\d{4} → 2024
{n,}n or more of the preceding element\d{2,} → 12, 123, 1234
{n,m}Between n and m of the preceding element\d{2,4} → 12, 123, 1234
*?Lazy zero or more (shortest match)<.*?> → matches one tag
+?Lazy one or more (shortest match)".+?" → "one" not "one" "two"

Anchors & Boundaries

PatternDescriptionExample
^Start of string (or line with m flag)^Hello → matches start
$End of string (or line with m flag)end$ → matches at end
\bWord boundary\bword\b → whole word only
\BNon-word boundary\Bword → "sword" not "word"

Groups & Capturing

PatternDescriptionExample
(abc)Capturing group — captures "abc"(\d+)-(\d+) → captures both
(?:abc)Non-capturing group — groups without capturing(?:https?)://
(?<name>abc)Named capturing group(?<year>\d{4})-(?<month>\d{2})
\1Backreference to group 1(\w)\1 → aa, bb, cc
(a|b)Alternation — matches a or b(cat|dog) → cat or dog

Lookahead & Lookbehind

PatternDescriptionExample
(?=abc)Positive lookahead — followed by "abc"\d(?=px) → 5 in "5px"
(?!abc)Negative lookahead — not followed by "abc"\d(?!px) → 5 in "5em"
(?<=abc)Positive lookbehind — preceded by "abc"(?<=\$)\d+ → 99 in "$99"
(?<!abc)Negative lookbehind — not preceded by "abc"(?<!\$)\d+ → 99 in "£99"

Flags

PatternDescriptionExample
gGlobal — find all matches, not just the first/a/g finds every "a"
iCase-insensitive matching/hello/i → Hello, HELLO
mMultiline — ^ and $ match line boundaries/^line/m per line
sDotall — . matches newline characters/.+/s spans lines
uUnicode — enables Unicode property escapes/\p{L}/u → letters
vUnicode sets — set operations in character classes/[\p{L}--[a-z]]/v

Escape Sequences

PatternDescriptionExample
\\Literal backslashC:\\ → C:\
\nNewlineline1\nline2
\tTabcol1\tcol2
\/Literal forward slash (in /regex/ delimiters)https:\/\/
\p{L}Unicode Letter property (requires u flag)\p{L}+ → any script
\p{N}Unicode Number property (requires u flag)\p{N}+ → any numeral

Test your patterns

Paste a regex pattern and test text to see highlighted matches and capture groups in real time.

Open Regex Tester