Regex Cheat Sheet
Every regular expression token you'll actually use — character classes, anchors, quantifiers, groups, lookarounds, flags, and escaping — with an example for each, plus ready-to-use patterns.
60 of 60 shown
| Pattern | Meaning | Example |
|---|---|---|
. | Any character except newlineCharacter classes | a.c → "abc", "a5c" |
\d | Any digit (0–9)Character classes | \d\d → "42" |
\D | Any non-digitCharacter classes | \D → "a", "#" |
\w | Word character (letters, digits, _)Character classes | \w+ → "user_1" |
\W | Non-word characterCharacter classes | \W → " ", "!" |
\s | Whitespace (space, tab, newline)Character classes | a\sb → "a b" |
\S | Non-whitespaceCharacter classes | \S+ → "hello" |
[abc] | Any one of a, b, or cCharacter classes | [aeiou] → a vowel |
[^abc] | Any character except a, b, cCharacter classes | [^0-9] → non-digit |
[a-z] | Any character in the rangeCharacter classes | [a-z] → lowercase letter |
^ | Start of string (or line)Anchors & boundaries | ^Hello matches at start |
$ | End of string (or line)Anchors & boundaries | end$ matches at end |
\b | Word boundaryAnchors & boundaries | \bcat\b → "cat" not "category" |
\B | Non-word boundaryAnchors & boundaries | \Bcat\B → "cat" inside "locator" |
\A | Start of string (PCRE/Python)Anchors & boundaries | anchors to the very start |
\Z | End of string (PCRE/Python)Anchors & boundaries | anchors to the very end |
* | 0 or moreQuantifiers | ab* → "a", "abbb" |
+ | 1 or moreQuantifiers | ab+ → "ab", "abbb" |
? | 0 or 1 (optional)Quantifiers | colou?r → "color", "colour" |
{n} | Exactly n timesQuantifiers | \d{4} → "2026" |
{n,} | n or more timesQuantifiers | \d{2,} → "42", "4200" |
{n,m} | Between n and m timesQuantifiers | \d{2,4} → 2 to 4 digits |
*?+??? | Lazy — match as few as possibleQuantifiers | <.+?> → shortest tag |
(...) | Capturing groupGroups & capturing | (ab)+ captures "ab" |
(?:...) | Non-capturing groupGroups & capturing | (?:ab)+ groups without capturing |
(?<name>...) | Named capturing groupGroups & capturing | (?<year>\d{4}) |
\1\2 | Backreference to a groupGroups & capturing | (\w)\1 → doubled letter "ll" |
\k<name> | Backreference by nameGroups & capturing | \k<year> |
a|b | Alternation (a OR b)Groups & capturing | cat|dog → "cat" or "dog" |
(?=...) | Positive lookaheadLookarounds | \d(?=px) → digit before "px" |
(?!...) | Negative lookaheadLookarounds | \d(?!px) → digit not before "px" |
(?<=...) | Positive lookbehindLookarounds | (?<=\$)\d+ → number after "$" |
(?<!...) | Negative lookbehindLookarounds | (?<!\$)\d+ → number not after "$" |
g | Global — find all matches, not just the firstFlags & modifiers | — |
i | Ignore case — case-insensitive matchingFlags & modifiers | — |
m | Multiline — ^ and $ match line starts/endsFlags & modifiers | — |
s | Dotall — . also matches newlinesFlags & modifiers | — |
u | Unicode — full Unicode matchingFlags & modifiers | — |
y | Sticky — match from lastIndex only (JS)Flags & modifiers | — |
x | Extended — ignore whitespace in the pattern (PCRE/Python)Flags & modifiers | — |
\. | A literal dotEscaping | — |
\* | A literal asteriskEscaping | — |
\? | A literal question markEscaping | — |
\+ | A literal plusEscaping | — |
\(\) | Literal parenthesesEscaping | — |
\[\] | Literal bracketsEscaping | — |
\{\} | Literal bracesEscaping | — |
\| | A literal pipeEscaping | — |
\\ | A literal backslashEscaping | — |
^\d+$ | Digits onlyReady-to-use patterns | — |
^[A-Za-z]+$ | Letters onlyReady-to-use patterns | — |
^[A-Za-z0-9]+$ | AlphanumericReady-to-use patterns | — |
^[^\s@]+@[^\s@]+\.[^\s@]+$ | Simple emailReady-to-use patterns | — |
^https?:\/\/[^\s/$.?#].[^\s]*$ | URL (http/https)Ready-to-use patterns | — |
^[a-z0-9]+(?:-[a-z0-9]+)*$ | SlugReady-to-use patterns | — |
^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$ | Hex colorReady-to-use patterns | — |
^\d{4}-\d{2}-\d{2}$ | Date (YYYY-MM-DD)Ready-to-use patterns | — |
^([01]\d|2[0-3]):[0-5]\d$ | Time (24h)Ready-to-use patterns | — |
^(?:(?:25[0-5]|2[0-4]\d|1?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|1?\d?\d)$ | IPv4 addressReady-to-use patterns | — |
^\s+|\s+$ | Leading/trailing whitespaceReady-to-use patterns | — |
Golden rules for writing regex:
- Be as specific as possible — vague greedy patterns like
.*cause surprising matches. Prefer character classes and lazy quantifiers. - Anchor when validating — wrap validation patterns in
^...$so they match the whole string, not just part of it. - Escape literal metacharacters — a bare
.matches any character, not a dot. - Test before you ship — try your pattern against edge cases in the Regex Tester and watch for catastrophic backtracking.
- Comment complex patterns — the
xflag (or your language's equivalent) lets you add whitespace and comments.
Your Data Never Leaves Your Device
Every tool runs entirely in your browser. Nothing you type is uploaded, stored, or logged on our servers.
Regular expressions (regex) are patterns used to match, search, and replace text. They power everything from form validation to find-and-replace in your editor. This regex cheat sheet groups the syntax you'll actually use — character classes, anchors, quantifiers, groups, lookarounds, and flags — with a short description and example for each, plus a set of ready-to-use patterns. Search by token, by meaning, or by what you're trying to match.
Regex syntax varies slightly between engines (JavaScript, PCRE, Python, and others). This sheet focuses on the common core; engine-specific tokens such as \A, \Z, and the x flag are marked where they matter.
Treat the ready-to-use patterns as starting points and always test them against your own data — “perfect” validation patterns, especially for email, are famously tricky.
FAQ
Greedy quantifiers (*, +) match as much text as possible, while lazy ones (*?, +?) match as little as possible. Use lazy quantifiers when you want the shortest match, such as the contents of a single HTML tag.
A capturing group (...) stores its match for reuse or extraction. A non-capturing group (?:...) groups a pattern for quantifiers or alternation without storing it, which is slightly faster and keeps your capture numbering clean.
Add the i flag. For example, /hello/i matches "Hello", "HELLO", and "hello".
The g flag finds every match in the string instead of stopping at the first. It's required for operations like replacing all occurrences.
Use them to assert context without consuming characters — for example, matching a number only when it follows a $ sign, without including the $ in the result.
Fully validating email and URLs with a single regex is notoriously hard. For production, use a simple pattern for a sanity check and rely on real validation (like sending a confirmation email) instead of a "perfect" regex.