DevToolbox
Quick Reference

Regex Cheat Sheet

A complete reference for regular expression syntax — character classes, quantifiers, anchors, groups, lookaheads, flags, and 12 copy-paste patterns for common validation tasks.

Try these patterns live in the Regex Tester →

Character Classes

Character classes match a single character from a defined set. Square brackets create custom sets; backslash shortcuts provide common presets.

.
Any character
Matches any character except newline (use the s flag to include newlines)
h.t → hat, hit, hot
\d
Digit
Matches any digit character 0–9
\d+ → 42, 100, 7
\D
Non-digit
Matches any character that is not a digit
\D+ → hello, abc
\w
Word character
Matches [a-zA-Z0-9_] — letters, digits, and underscore
\w+ → foo_bar, hello123
\W
Non-word
Matches any character not in [a-zA-Z0-9_]
\W → space, !, @
\s
Whitespace
Matches space, tab (\t), newline (\n), carriage return (\r), form feed (\f)
hello\sworld → hello world
\S
Non-whitespace
Matches any character that is not whitespace
\S+ → word, 123
[abc]
Character class
Matches any one character listed inside brackets
[aeiou] → a, e, i, o, u
[^abc]
Negated class
Matches any character NOT listed inside brackets
[^0-9] → a, b, !
[a-z]
Range
Matches any character within the specified range
[a-z] → a, m, z

Quantifiers

Quantifiers control how many times the preceding token must appear. Add ? after any quantifier to make it lazy (match as few characters as possible).

*
Zero or more
Matches 0 or more occurrences of the preceding token
ab* → a, ab, abbb
+
One or more
Matches 1 or more occurrences of the preceding token
ab+ → ab, abbb (not a)
?
Optional
Matches 0 or 1 occurrence — makes the preceding token optional
colou?r → color, colour
{n}
Exactly n
Matches exactly n occurrences of the preceding token
\d{4} → 2024, 1999
{n,}
n or more
Matches n or more occurrences
\d{2,} → 42, 100, 9999
{n,m}
Between n and m
Matches between n and m occurrences (inclusive)
\d{2,4} → 42, 100, 9999

Anchors

Anchors match positions in the string rather than actual characters. They are zero-width assertions.

^
Start of string
Matches at the start of the string. In multiline mode, matches at the start of each line
^Hello → 'Hello world' (yes), 'Say Hello' (no)
$
End of string
Matches at the end of the string. In multiline mode, matches at the end of each line
end$ → 'the end' (yes), 'ending' (no)
\b
Word boundary
Matches the position between a word character and a non-word character
\bcat\b → 'the cat sat' (yes), 'concatenate' (no)
\B
Non-word boundary
Matches any position that is NOT a word boundary
\Bcat\B → 'concatenate' (yes), 'the cat' (no)

Groups & References

Groups let you apply quantifiers to multiple tokens, capture submatches for later use, and write lookahead/lookbehind assertions.

(...)
Capturing group
Groups tokens and captures the match for backreferences
(foo)+ → foo, foofoo
(?:...)
Non-capturing group
Groups tokens without capturing — no reference created
(?:foo)+ → foo, foofoo
(?=...)
Positive lookahead
Asserts what follows the current position without consuming characters
\d+(?= dollars) → '100 dollars'
(?!...)
Negative lookahead
Asserts what does NOT follow the current position
\d+(?! dollars) → '100 euros'
(?<=...)
Positive lookbehind
Asserts what precedes the current position (ES2018+)
(?<=\$)\d+ → '$100'
(?<!...)
Negative lookbehind
Asserts what does NOT precede the current position (ES2018+)
(?<!\$)\d+ → '100 euros'
\1
Backreference
Refers to the text matched by the first capturing group
(\w+)\s\1 → 'the the'

Flags

Flags modify how the entire pattern is interpreted. In JavaScript, flags are appended after the closing slash: /pattern/gi

g
Global
Find all matches in the string, not just the first one
i
Case-insensitive
Match letters regardless of uppercase or lowercase
m
Multiline
Make ^ and $ match start and end of each line, not just the whole string
s
DotAll
Make the . metacharacter also match newline characters
u
Unicode
Enable full Unicode matching, required for Unicode escapes like \u{1F600}
y
Sticky
Only match starting from the lastIndex position in the target string

Common Patterns

Ready-to-use regex patterns for the most common validation and extraction tasks. Test them in the Regex Tester before using in production.

Email address
[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}
Example: user@example.com
URL (http/https)
https?:\/\/[^\s/$.?#].[^\s]*
Example: https://devtool.run/regex-tester
IPv4 address
(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)
Example: 192.168.1.1
Date (YYYY-MM-DD)
\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])
Example: 2026-03-20
Hex color
#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})\b
Example: #ea580c or #fff
US phone number
\+?1?[\s.\-]?\(?\d{3}\)?[\s.\-]?\d{3}[\s.\-]?\d{4}
Example: (555) 123-4567
ZIP code (US)
\d{5}(?:-\d{4})?
Example: 90210 or 90210-1234
Username (alphanumeric)
^[a-zA-Z0-9_]{3,20}$
Example: user_123
Strong password
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%]).{8,}$
Example: Must contain uppercase, lowercase, digit, symbol
HTML tag
<([a-z][a-z0-9]*)(?:[^>]*)?>.*?<\/\1>
Example: <div class="x">content</div>
ISO 8601 datetime
\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z?
Example: 2026-03-20T09:00:00Z
Semantic version
\d+\.\d+\.\d+(?:-[a-zA-Z0-9.]+)?
Example: 1.2.3 or 2.0.0-beta.1

Test your regex patterns

Paste any pattern from this cheat sheet and see real-time match highlighting with group extraction.

Open Regex Tester →

Regex FAQ

Answers to the most common questions about regular expression syntax and usage.

What is a regular expression (regex)?+

A regular expression is a sequence of characters that defines a search pattern. They are used for string matching, validation, extraction, and replacement in programming languages like JavaScript, Python, Java, and many others.

What is the difference between .* and .+?+

.* matches zero or more of any character, while .+ matches one or more. Use .+ when the content must be present, and .* when the match may be empty.

What does the ^ anchor do in regex?+

The ^ anchor matches the beginning of a string (or line in multiline mode). For example, ^hello matches 'hello world' but not 'say hello'. Inside a character class like [^abc], it negates the class instead.

What is a non-capturing group (?:...)?+

(?:...) groups part of a pattern without creating a capture group. This is useful when you need to apply a quantifier to multiple tokens but do not need to reference the matched text later. It is also slightly more performant than a capturing group.

What are regex lookaheads and lookbehinds?+

Lookaheads (?=...) and (?!...) assert whether a pattern does or does not follow the current position without consuming characters. Lookbehinds (?<=...) and (?

What regex flags are available in JavaScript?+

JavaScript supports: g (global – find all matches), i (case-insensitive), m (multiline – ^ and $ match line boundaries), s (dotAll – . matches newlines), u (unicode – full Unicode support), and y (sticky – matches from lastIndex only).