Complete regular expression reference with quick tester
| Pattern | Description | Example |
|---|---|---|
. | Any character except newline | a.c → abc, a1c |
\d | Digit [0-9] | \d{3} → 123 |
\D | Not a digit | \D+ → hello |
\w | Word char [a-zA-Z0-9_] | \w+ → hello_42 |
\W | Not a word character | \W → @, ! |
\s | Whitespace | a\sb → a b |
\S | Not whitespace | |
[abc] | Character set (a, b, or c) | [aeiou] |
[^abc] | Negated set (not a, b, c) | [^0-9] |
[a-z] | Range | [A-Za-z] |
| Pattern | Description |
|---|---|
* | 0 or more |
+ | 1 or more |
? | 0 or 1 (optional) |
{n} | Exactly n times |
{n,} | n or more |
{n,m} | Between n and m |
*? +? | Lazy (non-greedy) versions |
| Pattern | Description |
|---|---|
^ | Start of string/line |
$ | End of string/line |
\b | Word boundary |
\B | Not a word boundary |
| Pattern | Description |
|---|---|
(abc) | Capturing group |
(?:abc) | Non-capturing group |
(?<name>abc) | Named group |
\1 | Backreference to group 1 |
a|b | Alternation (a or b) |
(?=abc) | Positive lookahead |
(?!abc) | Negative lookahead |
(?<=abc) | Positive lookbehind |
(?<!abc) | Negative lookbehind |
| Flag | Description |
|---|---|
g | Global — find all matches |
i | Case-insensitive |
m | Multiline (^ and $ match line boundaries) |
s | Dotall (. matches newlines) |
u | Unicode support |
| Use Case | Pattern |
|---|---|
[\w.-]+@[\w.-]+\.\w{2,} | |
| URL | https?://[\w.-]+(?:/[\w./?%&=-]*)? |
| Phone (US) | \(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4} |
| IP Address | \b\d{1,3}(\.\d{1,3}){3}\b |
| Date (YYYY-MM-DD) | \d{4}-\d{2}-\d{2} |
| Hex Color | #[0-9a-fA-F]{3,8} |
| HTML Tag | <[^>]+> |
A comprehensive regular expression reference covering character classes, quantifiers, anchors, groups, lookaheads, flags, and common patterns. Includes a quick inline tester. Bookmark this page for quick regex reference while coding.
Regular expressions are the Swiss Army knife of text processing — powerful but notoriously hard to remember. This cheat sheet organizes every common regex pattern into logical categories with clear explanations and examples. Whether you're matching email addresses, parsing log files, validating input, or doing find-and-replace in your IDE, having a quick reference saves time and frustration. Patterns are organized by type: character classes (\d, \w, \s), quantifiers (*, +, ?, {n}), anchors (^, $, \b), groups and lookaheads, and common real-world patterns. Each entry includes the syntax, what it matches, and a practical example. Bookmark this page — you'll come back to it more often than you think.
The global flag 'g' finds all matches in the string, not just the first one. Without 'g', regex stops after the first match. Other common flags: 'i' (case-insensitive), 'm' (multiline), 's' (dotAll).
'.*' is greedy — it matches as much as possible. '.*?' is lazy — it matches as little as possible. For 'hello world', greedy '.*' matches everything; lazy '.*?' matches each tag separately.
Escape special characters with a backslash: \. matches a literal dot, \[ matches a literal bracket. Inside a character class [.] the dot is already literal. The special characters that need escaping are: . * + ? ^ $ { } [ ] ( ) | \.
Lookahead (?=...) matches a position followed by the pattern. Lookbehind (?<=...) matches a position preceded by the pattern. They don't consume characters — they just check if the pattern exists. 'foo(?=bar)' matches 'foo' only when followed by 'bar'.
The core syntax is similar but flavors differ. JavaScript lacks lookbehinds in older engines and atomic groups. Python supports named groups (?P