Skip to content
briskly.tools
· briskly / regex tester / cheat sheet

· reference · regex

Regex cheat sheet.

Every metacharacter, quantifier, anchor, character class, group type, and look-around — with examples you can paste into the live tester. ECMAScript flavor; differences called out where Python, PCRE, or Java diverge.

Metacharacters

The single-character building blocks. Most regex patterns are made of these plus a few quantifiers and groups.

SyntaxMeaningExample
.Any character except newline (unless s flag is set)./a.c/ → matches abc, a c, a9c
\dAny digit — equivalent to [0-9]./\d+/ → matches 42, 2026
\DAny non-digit — equivalent to [^0-9]./\D+/ → matches letters, spaces, symbols
\wAny word character — equivalent to [A-Za-z0-9_]./\w+/ → matches hello_42
\WAny non-word character./\W/ → matches spaces, punctuation
\sAny whitespace — space, tab, newline, etc./\s+/ → matches run of whitespace
\SAny non-whitespace./\S+/ → matches runs of text without spaces
\bWord boundary — zero-width assertion between \w and \W./\bword\b/ → matches word but not swords
\BNon-word boundary./\Bing\b/ → matches ing inside a word
\\A literal backslash./C:\\Users/ → matches C:\Users

Quantifiers

How many of the preceding token to match. Every quantifier has a greedy default (match as much as possible) and a lazy variant with a trailing ? (match as little as possible).

SyntaxMeaningExample
*Zero or more of the preceding token — greedy./a*/ → matches '', a, aa, aaaa
+One or more — greedy./a+/ → matches a, aa, aaaa (not empty)
?Zero or one — greedy./colou?r/ → matches color and colour
{n}Exactly n of the preceding token./\d{3}/ → matches 123 but not 12 or 1234+
{n,}n or more./a{2,}/ → matches aa, aaa, aaaa
{n,m}Between n and m inclusive — greedy./\d{2,4}/ → matches 12, 123, 1234
*?Zero or more — lazy (as few as possible)./<.*?>/ → matches <a>, not <a>...<b>
+?One or more — lazy./".+?"/ → matches each quoted string separately
??Zero or one — lazy./a??/ → prefers to match empty

Character classes

Match any character from a set. Negate with a leading ^. Use ranges for contiguous sets. Unicode property escapes require the u flag.

SyntaxMeaningExample
[abc]Any one of a, b, or c./[aeiou]/ → matches any vowel
[^abc]Any character except a, b, or c./[^0-9]/ → matches any non-digit
[a-z]Any lowercase letter (range)./[a-zA-Z]/ → any letter
[0-9]Any digit (range)./[0-9]{3}-[0-9]{4}/ → US phone
\p{L}Unicode category — any letter. Requires u flag./\p{L}+/u → any Unicode word
\p{N}Unicode category — any number. Requires u flag./\p{N}+/u → any Unicode digit

Anchors

Zero-width assertions — they match positions, not characters. Useful for binding patterns to line starts, ends, or word boundaries.

SyntaxMeaningExample
^Start of string (or start of line with m flag)./^Hello/ → matches at the beginning
$End of string (or end of line with m flag)./world$/ → matches at the end
\AStart of string only — not supported in JS regex; use ^ without m flag.
\zEnd of string only — not supported in JS regex; use $ without m flag.

Groups and captures

Groups serve three purposes: applying quantifiers to multiple tokens, capturing a substring you can reference later, and structuring alternation.

SyntaxMeaningExample
(abc)Capturing group — referenced as $1, $2, …/(\w+)@(\w+)/ → $1 = user, $2 = domain
(?:abc)Non-capturing group — grouping without capture./(?:ab)+/ → matches abab, no capture
(?<name>abc)Named capture — referenced as $<name>./(?<year>\d{4})/ → .groups.year
\1 \2Backreference to a previous capture./(\w)\1/ → matches doubled letters
\k<name>Named backreference./(?<q>['"]).*?\k<q>/ → balanced quotes

Look-around

Zero-width assertions that check what's around the current position without consuming characters. JavaScript supports all four variants since ES2018.

SyntaxMeaningExample
(?=abc)Positive lookahead — followed by abc (not consumed)./foo(?=bar)/ → matches foo before bar
(?!abc)Negative lookahead — not followed by abc./foo(?!bar)/ → matches foo NOT before bar
(?<=abc)Positive lookbehind — preceded by abc./(?<=\$)\d+/ → digits after $
(?<!abc)Negative lookbehind — not preceded by abc./(?<!\$)\d+/ → digits NOT after $

Flags

Modify how the pattern matches. In JavaScript regex literal form, flags go after the closing /: /pattern/gi.

SyntaxMeaningExample
gGlobal — find all matches, not just the first./\d/g on '123' matches 3 times
iIgnore case./cat/i matches Cat, CAT, cAt
mMultiline — ^ and $ match line boundaries./^end$/m on multi-line input
sDotall — the . metacharacter also matches newlines./start.*end/s spans lines
uUnicode — enables \p{...} and proper surrogate handling./\p{Emoji}/u matches emoji
ySticky — each match must start exactly at lastIndex.Used in parsers for tokenizing

Replacement substitutions

In replacement strings (for .replace()or our tester's replace mode), these special tokens are substituted with captured values.

SyntaxMeaningExample
$&The whole match.'hello'.replace(/./g, '$&_') → 'h_e_l_l_o_'
$1, $2, …Numbered capture groups.'John Smith' → /(\w+) (\w+)/ → '$2, $1' = 'Smith, John'
$<name>Named capture group./(?<year>\d{4})/ → '$<year>'
$`The portion before the match.Prefix before the matched substring.
$'The portion after the match.Suffix after the matched substring.
$$A literal dollar sign.'USD'.replace(/USD/, '$$') → '$'

Common patterns

Production-grade regexes for the tasks everyone writes ten times. Copy-paste into the tester to see each one match.

· US phone number (loose)

^(?:\(\d{3}\)|\d{3})[- ]?\d{3}[- ]?\d{4}$

Matches 5551234567, 555-123-4567, (555) 123-4567. Doesn't validate area code.

· Email (loose — good enough for UX validation)

^[^\s@]+@[^\s@]+\.[^\s@]+$

Catches obvious typos. For production, send a verification email — the RFC-compliant regex is ~300 chars.

· URL

^https?:\/\/[^\s/$.?#].[^\s]*$

Matches http:// or https:// followed by a hostname + path.

· ISO date (YYYY-MM-DD)

^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$

Validates shape; doesn't catch Feb 30 or Apr 31.

· Hex color

^#(?:[0-9a-fA-F]{3}){1,2}$

Matches #abc and #aabbcc; not 4/8-digit alpha variants.

· IPv4 address (strict)

^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)$

Validates each octet is 0-255.

· Whitespace (collapse runs to one space)

\s+

Replace with ' ' to normalize whitespace.

· Leading/trailing whitespace (trim)

^\s+|\s+$

Replace with '' for a regex-based trim.

Related