TL;DR
- → Regex looks intimidating but most real-world use cases need only a dozen patterns.
- → This cheat sheet covers character classes, anchors, quantifiers, and groups.
- → Test any pattern live with the free Regex Tester — real-time highlighting.
Advertisement Space
Regular expressions look like a keyboard fell down the stairs. But the truth is, most developers use the same 15–20 patterns repeatedly. Once those click, you can read and write regex for most real-world tasks without needing to memorise the full spec.
This is a practical reference — not an exhaustive one. Use the Regex Tester alongside this post to test any pattern against real input in real time.
Character Classes
The most-used shorthand character classes:
| Pattern | Matches | Example |
|---|---|---|
| \d | Any digit (0–9) | \d+ matches 42 or 9000 |
| \D | Any non-digit | \D+ matches hello |
| \w | Word char (a–z, A–Z, 0–9, _) | \w+ matches hello_world |
| \W | Non-word character | \W matches spaces, punctuation |
| \s | Whitespace (space, tab, newline) | \s+ matches gaps between words |
| \S | Non-whitespace | \S+ matches individual words |
| . | Any character except newline | h.t matches hat, hit, hot |
| [abc] | One of a, b, or c | [aeiou] matches any vowel |
| [^abc] | Anything NOT a, b, or c | [^0-9] matches non-digits |
| [a-z] | Any lowercase letter | [a-zA-Z] matches any letter |
Anchors
Anchors don't match characters — they match positions in the string.
| Pattern | Meaning |
|---|---|
| ^ | Start of string (or line in multiline mode) |
| $ | End of string (or line in multiline mode) |
| \b | Word boundary — position between a word char and a non-word char |
| \B | Non-word boundary |
Quantifiers
Quantifiers control how many times the preceding token must match.
| Quantifier | Meaning |
|---|---|
| * | 0 or more (greedy) |
| + | 1 or more (greedy) |
| ? | 0 or 1 — makes preceding token optional |
| {n} | Exactly n times |
| {n,} | n or more times |
| {n,m} | Between n and m times |
| *? +? ?? | Lazy versions — match as few as possible |
Groups and Alternation
| Syntax | Meaning |
|---|---|
| (abc) | Capturing group — captures 'abc' for back-referencing |
| (?:abc) | Non-capturing group — groups without capturing |
| a|b | Alternation — matches a or b |
| (?=abc) | Lookahead — matches if followed by 'abc' (doesn't consume) |
| (?!abc) | Negative lookahead — matches if NOT followed by 'abc' |
Patterns You'll Actually Use
Copy these directly. Test and tweak them in the Regex Tester.
Email (basic)
^[\w.+\-]+@[\w\-]+\.[a-zA-Z]{2,}$
UK postcode
^[A-Z]{1,2}[0-9][0-9A-Z]?\s?[0-9][A-Z]{2}$
US phone number
^(\+1)?[\s\-]?\(?\d{3}\)?[\s\-]?\d{3}[\s\-]?\d{4}$
URL (http/https)
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&/=]*)
IPv4 address
^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$
Hex colour code
^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$
Slug (URL-safe string)
^[a-z0-9]+(?:-[a-z0-9]+)*$
Strong password
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\w\s]).{8,}$
ISO date (YYYY-MM-DD)
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$
Trim whitespace
^\s+|\s+$
Flags
Flags modify how the entire pattern behaves. Add them after the closing slash: /pattern/gi
| Flag | Effect |
|---|---|
| g | Global — find all matches, not just the first |
| i | Case-insensitive — a matches A |
| m | Multiline — ^ and $ match start/end of each line |
| s | Dotall — . matches newlines too |
Frequently Asked Questions
What does .* mean in regex?
. matches any single character except a newline. * means 'zero or more of the preceding token'. So .* together means 'match any character, any number of times' — it's a greedy wildcard. It will match as much as possible. For example, in the string 'hello world', .* matches the entire string. Use .*? (lazy) if you want the shortest possible match.
What is the difference between + and * in regex?
* means zero or more occurrences of the preceding character or group. + means one or more occurrences. The practical difference: * will match even if the character isn't present at all, while + requires at least one match. For example, \d* matches an empty string or any number of digits, while \d+ requires at least one digit.
What does ^ mean in regex?
^ has two meanings depending on context. At the start of a pattern (e.g. ^hello), it anchors the match to the beginning of the string — only matches if the string starts with 'hello'. Inside a character class (e.g. [^abc]), it negates the class — matches any character that is NOT a, b, or c. The context always makes it clear which meaning applies.
How do I match an email address with regex?
A regex that matches most valid email addresses: ^[\w.+\-]+@[\w\-]+\.[a-zA-Z]{2,}$. This covers the format local-part@domain.tld. It won't catch 100% of edge cases in the RFC 5321 spec (which allows unusual formats like quoted strings), but it handles every real-world email format you'll encounter. For production, always validate email by sending a confirmation — regex alone is not enough.
What is a capturing group in regex?
A capturing group is created with parentheses: (pattern). It does two things: it groups part of the pattern together (like brackets in maths), and it captures the matched text so you can reference it later. In most languages, you access captured groups as $1, $2 (for replace operations) or match[1], match[2] (in code). Use (?:pattern) for a non-capturing group if you only need the grouping, not the capture.