A Regex Cheatsheet for Everyday Text Work
The 12 regex patterns that cover 90% of real-world cleanup tasks — emails, phone numbers, slugs, whitespace, duplicates.
You do not need to memorize regex. You need a short list of patterns that solve the problems you actually hit.
The 12 patterns to know
| Goal | Pattern | |
|---|---|---|
| Trim whitespace | `^\s+ | \s+$` |
| Collapse spaces | \s{2,} → | |
| Strip HTML tags | <[^>]+> | |
| Email (loose) | [\w.+-]+@[\w-]+\.[\w.-]+ | |
| URL | https?://[^\s)]+ | |
| US phone | \(?\d{3}\)?[ .-]?\d{3}[ .-]?\d{4} | |
| Numbers only | \d+ | |
| Decimals | -?\d+(\.\d+)? | |
| Slug-safe chars | [^a-z0-9-] | |
| Leading zeros | ^0+ | |
| Markdown links | \[([^\]]+)\]\(([^)]+)\) | |
| Hex color | #?[0-9a-fA-F]{6} |
Test before you ship
Always run a regex against real data before you trust it. Our Regex Tester shows live matches as you type and supports flags (g, i, m, s).
When to NOT use regex
Some problems look like regex jobs but are not:
- Parsing HTML or JSON — use a real parser
- Validating emails strictly — the full RFC is hundreds of lines; use a library or send a confirmation email
- Matching nested brackets — regex cannot count
Quick combos
- Clean a pasted phone list: strip non-digits with
\D+, then reformat - Make a slug: lowercase, replace
\s+with-, then strip[^a-z0-9-] - Find duplicate lines: sort the file, then
^(.*)\n\1$
Most cleanup jobs are 2–3 of these patterns in sequence. Build them up one at a time.