Aregex cheat sheetsaves you from re-reading documentation every time you write a pattern. This reference covers the syntax you use every day — character classes, anchors, quantifiers, groups, and lookaheads — with short examples you can copy and test right away.
Every pattern here is written for JavaScript RegExp and is widely familiar in other hosts. Paste any example into theRegex Visualizer to see matches highlighted in real time — the tool matches with JavaScript RegExp only.
Character classes
Character classes match a single character from a defined set. They are the building blocks of almost every pattern.
PatternWhat it matches
.
Any character except a newline
\d
A digit (0–9)
\D
Any character that is not a digit
\w
A word character: letter, digit, or underscore
\W
Any character that is not a word character
\s
A whitespace character (space, tab, newline)
\S
Any non-whitespace character
[abc]
One of: a, b, or c
[^abc]
Any character except a, b, or c
[a-z]
Any lowercase letter a through z
[A-Z0-9]
Any uppercase letter or digit
Anchors
Anchors match a position in the text, not a character. Use them to pin your pattern to the start or end of a string, or to word boundaries.
PatternWhat it matches
^
Start of the string (or line in multiline mode)
$
End of the string (or line in multiline mode)
\b
Word boundary — between a word char and a non-word char
\B
Not a word boundary
Common mistake:using^and$when you want to find a match inside a longer string. Those anchors require the whole string to match the pattern. Remove them if you want to search within text.
Quantifiers
Quantifiers say how many times the preceding element must appear.
PatternMeaning
*
Zero or more times
+
One or more times
?
Zero or one time (makes the element optional)
{n}
Exactly n times
{n,}
At least n times
{n,m}
Between n and m times
By default, quantifiers aregreedy— they match as many characters as possible. Add a?after any quantifier to make itlazy(matches as few as possible):.*?,+?,{2,5}?.
Groups and alternation
PatternWhat it does
(abc)
Capturing group — captures the match so you can reuse it
(?:abc)
Non-capturing group — groups without capturing
(?<name>abc)
Named capturing group
a|b
Alternation — match a or b
\1
Back-reference to the first capturing group
Lookaheads and lookbehinds
Lookarounds match a position without consuming characters. They check what comes before or after the current position without including it in the match.
PatternWhat it does
(?=abc)
Positive lookahead — position is followed by "abc"
(?!abc)
Negative lookahead — position is not followed by "abc"
(?<=abc)
Positive lookbehind — position is preceded by "abc"
(?<!abc)
Negative lookbehind — position is not preceded by "abc"
Flags
Flags change how the whole pattern behaves. In JavaScript, you add them after the closing slash:/pattern/flags.
FlagWhat it does
g
Global — find all matches, not just the first
i
Case-insensitive — treat uppercase and lowercase the same
m
Multiline —^and$match line starts/ends
s
Dotall —.matches newlines too
u
Unicode — enables full Unicode support
Common regex patterns
These are ready-to-use patterns for tasks that come up often. Paste them intoRegex Visualizer to test against your own text.
- Email address:
[a-zA-Z0-9._%+\\-]+@[a-zA-Z0-9.\\-]+\\.[a-zA-Z]{2,} - URL:
{"https?:\\/\\/[\\w\\-._~:/?#[\\]@!$&'()*+,;=]+"} - Date (YYYY-MM-DD):
\\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\\d|3[01]) - IPv4 address:
(?:\\d{1,3}\\.){3}\\d{1,3} - Hex color:
#(?:[0-9a-fA-F]{3}){1,2} - HTML tag:
<([a-z]+)[^>]*>.*?<\\/\\1> - Whitespace (trim):
^\\s+|\\s+$ - Duplicate words:
\\b(\\w+)\\s+\\1\\b
How to test a regex pattern
- OpenRegex Visualizer — it runs in your browser, nothing is sent anywhere.
- Paste your pattern into the pattern field.
- Type or paste your test text in the text area below.
- Matches highlight in real time. Hover a match to see which groups captured what.
- Switch flags (global, case-insensitive, multiline) from the toolbar to see how the results change.
Testing before you use a pattern in code saves hours of debugging. A pattern that looks correct on paper often behaves differently on real data — especially when the text contains accented characters, line breaks, or unexpected whitespace.
Tips for writing better patterns
- Start specific, then loosen:begin with a tight pattern and relax it until it covers your real cases. A pattern that is too loose matches things it should not.
- Anchor when you mean the whole string:use
^and$only when the pattern must match the full input. - Prefer
\dover[0-9]:shorthand classes are shorter to read and match the same characters in most engines. - Use non-capturing groups when you do not need the value:
(?:abc)is faster than(abc)if you never use the captured text. - Test edge cases:empty strings, strings with only spaces, and strings with Unicode characters are common sources of bugs.





