← Blog

Regex Cheat Sheet: Patterns That Work

Regex visualizer blog · Open RegEx Visualizer

Regex cheat sheet with character classes, quantifiers, and pattern examples
Regex Cheat Sheet: Patterns That Work

A regex cheat sheet saves 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 works in JavaScript, Python, and most modern engines. You can paste any example into the Regex Visualizer and see the matches highlighted in real time.

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
\dA digit (0–9)
\DAny character that is not a digit
\wA word character: letter, digit, or underscore
\WAny character that is not a word character
\sA whitespace character (space, tab, newline)
\SAny 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)
\bWord boundary — between a word char and a non-word char
\BNot 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 are greedy — they match as many characters as possible. Add a ? after any quantifier to make it lazy (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|bAlternation — match a or b
\1Back-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
gGlobal — find all matches, not just the first
iCase-insensitive — treat uppercase and lowercase the same
mMultiline — ^ and $ match line starts/ends
sDotall — . matches newlines too
uUnicode — enables full Unicode support

Common regex patterns

These are ready-to-use patterns for tasks that come up often. Paste them into Regex 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

  1. Open Regex Visualizer — it runs in your browser, nothing is sent anywhere.
  2. Paste your pattern into the pattern field.
  3. Type or paste your test text in the text area below.
  4. Matches highlight in real time. Hover a match to see which groups captured what.
  5. 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 \d over [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.

FAQ

What is a regex cheat sheet?

A regex cheat sheet is a quick reference for regular expression syntax. It lists patterns like \d for digits, ^ for start of string, and * for zero or more, so you can write patterns without memorizing every rule.

What does \d mean in regex?

\d matches any single digit from 0 to 9. It is a shorthand for the character class [0-9]. Use \d+ to match one or more digits in a row.

What is the difference between * and + in regex?

* means zero or more — the element before it is optional and can appear any number of times. + means one or more — the element must appear at least once. For example, \d* matches an empty string, but \d+ requires at least one digit.

What does the g flag do in JavaScript regex?

The g (global) flag makes the pattern find all matches in the string instead of stopping after the first one. Without g, methods like match() return only the first result.

What is a capturing group in regex?

A capturing group, written as (pattern), stores the text matched inside it so you can reuse it later — in a replacement string or a back-reference. Use (?:pattern) if you want to group without capturing.

How do I make a regex case-insensitive?

Add the i flag to your pattern. In JavaScript: /pattern/i. In Python: re.compile('pattern', re.IGNORECASE). The pattern then matches uppercase and lowercase letters the same way.

For related reading, see how to test regex patterns online or browse the Regex visualizer blog.