2026-09-09
Regex Cheat Sheet
The regex syntax that comes up constantly, in one place. Paste any pattern from here into the Regex Tester to try it against real text before you commit to it.
Character classes
| Syntax |
Matches |
. |
Any character except a newline |
\d |
A digit (0-9) |
\D |
A non-digit |
\w |
A word character (letters, digits, underscore) |
\W |
A non-word character |
\s |
A whitespace character (space, tab, newline) |
\S |
A non-whitespace character |
[abc] |
Any one of a, b, or c |
[^abc] |
Any character that is not a, b, or c |
[a-z] |
Any character in the range a to z |
Anchors
| Syntax |
Matches |
^ |
Start of the string (or start of a line, with the m flag) |
$ |
End of the string (or end of a line, with the m flag) |
\b |
A word boundary |
\B |
Not a word boundary |
Quantifiers
| Syntax |
Matches |
* |
Zero or more of the preceding token |
+ |
One or more of the preceding token |
? |
Zero or one of the preceding token |
{n} |
Exactly n of the preceding token |
{n,} |
n or more of the preceding token |
{n,m} |
Between n and m of the preceding token |
*?, +?, ?? |
Lazy (non-greedy) versions of the above - match as little as possible |
Groups
| Syntax |
Purpose |
(abc) |
Capturing group - remembers the matched text |
(?:abc) |
Non-capturing group - groups without remembering |
(?<name>abc) |
Named capturing group |
| `(a |
b)` |
(?=abc) |
Positive lookahead - matches if followed by abc, without consuming it |
(?!abc) |
Negative lookahead - matches if not followed by abc |
(?<=abc) |
Positive lookbehind - matches if preceded by abc |
(?<!abc) |
Negative lookbehind - matches if not preceded by abc |
Common flags
| Flag |
Effect |
g |
Global - find all matches, not just the first |
i |
Case-insensitive matching |
m |
Multiline - ^/$ match the start/end of each line, not just the whole string |
s |
Dotall - . also matches newlines |
u |
Unicode mode |
Common patterns (with caveats)
These are the "close enough for most cases" patterns people reach for constantly - not RFC-complete validators. Each has real edge cases, noted below.
| Purpose |
Pattern |
Caveat |
| Email-ish |
^[\w.+-]+@[\w-]+\.[a-zA-Z]{2,}$ |
Real email addresses allow far more characters and formats than this; use it as a loose sanity check, not a validator |
| Digits only |
^\d+$ |
Rejects a leading +/- sign or decimal point - fine for IDs, not for general numbers |
| Whitespace trim |
`^\s+ |
\s+$(replace with''`) |
For anything security- or validation-critical (real email delivery, password rules, input sanitization), don't rely on a quick regex like these - use a proper library or your platform's built-in validation.
Try Regex Tester · More cheat sheets · All tools