Most regex cheat sheets are a wall of symbols with no proof that any of it works. This one is different in one boring but useful way: every example below was run in Python 3.12’s re module and in Node.js 24, and the output you see is the real output, copied from the terminal. Where the two languages disagree, and they do in five places we found, you’ll see both answers side by side. Use this regex cheat sheet as a reference, then paste any pattern into your own console to check it.
Quick answer: The regex syntax you’ll use most is \d (digit), \w (word character), \s (whitespace), . (any character but newline), ^ and $ (start and end), *, +, ? and {m,n} (repeats), ( ) (groups), | (or) and lookarounds like (?=...). Python and JavaScript share almost all of it, but not Unicode digits, named-group syntax or variable-length lookbehind.
- All 37 examples were run in Python 3.12.4 and Node 24.22.1; 32 gave identical output.
- Python’s
\dmatched the Arabic-Indic digit ٣; JavaScript’s didn’t. - A nested pattern like
(a+)+$took 17.5 seconds in Python on a 25-character string. An atomic group fixed it.
How to test these examples
In Python, always write patterns as raw strings, r"\d+". The official re module docs recommend it, because in a normal string Python handles backslashes first and invalid escapes now raise a SyntaxWarning. In JavaScript, use a regex literal like /\d+/g. The g flag means “find every match,” which is what re.findall or re.finditer do in Python.
# Python
import re
re.findall(r"\d+", "Order 66 shipped 3 items") # ['66', '3']
// JavaScript (Node or the browser console)
"Order 66 shipped 3 items".match(/\d+/g) // ['66', '3']
The browser console is the fastest place to try JavaScript patterns. Our Chrome DevTools hidden features guide covers a few console tricks that make this quicker.
Character classes
| Pattern | Matches | Example input | Real output |
|---|---|---|---|
\d+ | one or more digits | Order 66 shipped 3 items | 66, 3 |
\w+ | letters, digits, underscore | it’s 2026 | it, s, 2026 |
\s+ (split) | spaces, tabs, newlines | a␣␣b⇥c↵d | a, b, c, d |
a.c | any char except newline | abc a↵c | abc |
a.c + s flag | any char, newline too | abc a↵c | abc, a↵c |
[aeiou] + i flag | one of a set | Regular Expression | e, u, a, E, e, i, o |
[^0-9-] | anything not in the set | 555-01×9 | x |
Notice the second row. \w doesn’t include the apostrophe, so “it’s” splits in two. That catches people out when they count words with regex. Capital letters flip a class: \D is “not a digit,” \W is “not a word character,” \S is “not whitespace.”
Anchors and word boundaries
^ means start of the string and $ means end. Together they force the whole input to match, which is what you want for validation. ^\d{3}$ returned true for “123” and false for “1234” in both languages.
\b is a word boundary, the invisible spot between a word character and anything else. Searching “cat concat cats cat.” with \bcat\b found exactly two matches: the first word and the last one. “concat” and “cats” were skipped, which is usually what you meant.
With the m (multiline) flag, ^ and $ work per line instead of per string. That makes list parsing easy:
text = "Shopping:\n- milk\n- eggs"
re.findall(r"^- (.+)$", text, re.M) # ['milk', 'eggs']
Quantifiers: greedy vs lazy
| Quantifier | Means | Example | Real output |
|---|---|---|---|
? | 0 or 1 | colou?r on “color colour colr” | color, colour |
{2,4} | 2 to 4 times | \b\d{2,4}\b on “7 42 2026 123456” | 42, 2026 |
.+ | greedy: as much as possible | <.+> on “<b>bold</b>” | <b>bold</b> |
.+? | lazy: as little as possible | <.+?> on the same text | <b>, </b> |
Two more you’ll see everywhere: * means zero or more and + means one or more, both greedy by default. The greedy vs lazy pair is the classic surprise. A greedy .+ runs to the last possible > and swallows the whole tag pair. Adding ? after the quantifier makes it stop at the first one. (For real HTML, use a parser. Regex is fine for quick one-off cleanups.)

Groups, backreferences and alternation
Parentheses capture part of a match so you can reuse it. On “Due 2026-11-29”, (\d{4})-(\d{2})-(\d{2}) returned the groups 2026, 11 and 29. If you only need grouping and not capture, use (?:...): (?:ha)+ on “hahaha ha” gave “hahaha” and “ha”.
A backreference like \1 means “the same text group 1 matched.” Here’s a duplicate-word finder that actually works:
re.findall(r"\b(\w+)\s+\1\b", "the the cat sat sat down")
# findall returns the group: ['the', 'sat']
# full matches (finditer / JS matchAll): 'the the', 'sat sat'
The pipe | means “or.” Wrap it in a group so it doesn’t split your whole pattern: \b(?:cat|dog)s?\b on “cats, dogs and a catalog” matched “cats” and “dogs” and left “catalog” alone.
Lookahead and lookbehind
Lookarounds check what’s next to a match without including it. They’re the closest thing regex has to an “if.”
| Syntax | Name | Example | Real output |
|---|---|---|---|
x(?=y) | lookahead | \d+(?=px) on “12px 5em 30px” | 12, 30 |
x(?!y) | negative lookahead | q(?!u) on “Iraq quit qat” | q, q (from Iraq and qat) |
(?<=y)x | lookbehind | (?<=\$)\d+(?:\.\d\d)? on “$15.99 and $7, not 8” | 15.99, 7 |
(?<!y)x | negative lookbehind | (?<!-)\d+ | digits not after a minus |
Stack lookaheads at the start to check several rules at once. ^(?=.*\d)(?=.*[A-Z]).{8,}$ said true for “Secret123” and false for “secret123” (no capital). It’s a handy demo, though real password rules belong in code you can read.
Regex flags in Python and JavaScript
| What it does | Python | JavaScript |
|---|---|---|
| Ignore case | re.I | i |
| ^ and $ per line | re.M | m |
| Dot matches newline | re.S | s |
| Find all matches | use findall / finditer | g |
ASCII-only \d \w \s | re.A | default behaviour |
Unicode properties like \p{L} | not in re | u or v |
| Comments and spacing in pattern | re.X | no flag |
Practical regex patterns with examples
These are the patterns people search for most. Each ran in both languages with the same result.
# US phone numbers
r"\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}"
"Call (555) 010-4477 or 555.010.9999" -> ['(555) 010-4477', '555.010.9999']
# Hex colours (3 or 6 digits)
r"#(?:[0-9a-fA-F]{3}){1,2}\b"
"#fff #1A2B3C #12" -> ['#fff', '#1A2B3C']
# Reformat dates (Python \3, JavaScript $3)
re.sub(r"(\d{4})-(\d{2})-(\d{2})", r"\3/\2/\1", "2026-11-29") -> '29/11/2026'
# Collapse repeated spaces
re.sub(r" {2,}", " ", "too many spaces") -> 'too many spaces'
# Make a URL slug (lowercase first, then strip dashes at the ends)
re.sub(r"[^a-z0-9]+", "-", "regex cheat sheet: 2026 edition!") -> 'regex-cheat-sheet-2026-edition'
Two “recipes” from most cheat sheets failed in small but real ways. The common IPv4 pattern \b(?:\d{1,3}\.){3}\d{1,3}\b happily matched “999.300.1.1”, which isn’t a valid address. Check the numbers in code after the match. And https?://\S+ grabbed the full stop at the end of a sentence: “https://example.com/docs.” Ending the pattern with [^\s.,;:!?)] fixed it and returned “https://example.com/docs”.
Python vs JavaScript regex differences
This is where copying a pattern from a JavaScript answer into Python, or the other way round, goes wrong. Same pattern, same text, real output from both:
| Test | Python 3.12 | Node 24 |
|---|---|---|
\d on “٣ and 3” | ٣, 3 | 3 |
\w+ on “café naïve” | café, naïve | caf, na, ve |
(?P<year>\d{4}) | {‘year’: ‘2026’} | SyntaxError: Invalid group |
(?<year>\d{4}) | re.error (in 3.12) | {year: ‘2026’} |
(?<=\$\s*)\d+ | re.error: look-behind requires fixed-width pattern | 40, 5 |

The Unicode rows matter most. Python’s docs say that for str patterns \d matches “any Unicode decimal digit,” and re.ASCII narrows it to [0-9]; with that flag we got just “3”. MDN’s regex cheat sheet defines JavaScript’s \d as [0-9] and \w as the basic Latin alphabet plus digits and underscore. To match accented letters in JavaScript, use a Unicode property with the u flag: /[\p{L}\p{N}_]+/gu returned “café” and “naïve” in our run.
So if you validate “digits only” with \d in Python and then call int(), non-ASCII digits can slip through. Use [0-9] when you mean 0 to 9.
The part cheat sheets skip: a short pattern can hang your program. We timed (a+)+$ against a string of a’s ending in “b”, which can never match. In Python it took 0.6 seconds at 20 a’s, 2.1 seconds at 22 and 17.5 seconds at 24. Node took 3.5 seconds at 24. The harmless-looking a+$ stayed under a millisecond. Python 3.11 added atomic groups and possessive quantifiers for this: (?>a+)+$ and (a++)+$ finished the 24-character case in under half a millisecond. Node 24 rejected (?>...) as an invalid group, so in JavaScript you fix it by rewriting the pattern without nested repeats.
Those timings come from one run each on a Windows laptop that was busy with other jobs, so treat the exact milliseconds loosely. The shape is the point: every extra two characters made the bad pattern several times slower.

When to use regex, and when not to
Regex is great for finding, extracting and cleaning text with a clear shape: dates, IDs, log lines, repeated spaces. It’s a poor fit for nested formats like HTML or JSON, and for anything where a plain string method works. "x" in s and s.startswith("x") are easier to read than a pattern.
Bookmark the Python vs JavaScript table above, test every pattern on real input before you ship it, and add a comment saying what it’s for. In Python, re.X lets you spread a long pattern over several lines with comments, which your future self will thank you for.
Next: if you’re sharpening your Python, our Python tricks for beginners collects small habits like these, and the Python f-string tricks post pairs well with re.sub for formatting cleaned-up text. On the JavaScript side, see our JavaScript one-liners.
FAQ
What’s the difference between re.match and re.search?
re.match only checks the start of the string, re.search looks anywhere, and re.fullmatch needs the whole string to match. The current Python docs also add re.prefixmatch as the new name for re.match and mark match as soft deprecated from 3.15, though it isn’t being removed.
Why does my Python regex need an r before the string?
Without it, Python reads backslashes before the regex engine sees them, so "\b" becomes a backspace character. A raw string, r"\b", passes the backslash through untouched.
Can I copy a regex from JavaScript to Python?
Usually, yes. Check named groups ((?<name>) vs (?P<name>) in 3.12), lookbehind length, replacement syntax ($1 vs \1) and Unicode behaviour of \d and \w. Those caused every difference in our test.
Is there a regex cheat sheet PDF?
We haven’t made one. The tables on this page print cleanly from your browser’s print dialog, and the scripts we used are small enough to keep next to your code.