Regex has a fearsome reputation, but a small set of patterns covers the vast majority of everyday tasks. Here are ten worth memorising.
1. Email address
/^[^\s@]+@[^\s@]+\.[^\s@]+$/
Not RFC-5321-complete, but catches 99% of real-world invalid inputs without false positives.
2. URL
/https?:\/\/[^\s/$.?#].[^\s]*/i
Matches http and https URLs in free text. Useful for linkifying user content.
3. IPv4 address
/^(\d{1,3}\.){3}\d{1,3}$/
Quick syntactic check — pair with a range check (0–255) for full validation.
4. Hex colour
/^#([0-9a-f]{3}|[0-9a-f]{6})$/i
Matches both shorthand (#f0a) and full (#ff00aa) CSS colour literals.
5. Slug
/^[a-z0-9]+(?:-[a-z0-9]+)*$/
URL-safe lowercase slugs with hyphens but no leading/trailing/double hyphens.
6. ISO date
/^\d{4}-\d{2}-\d{2}$/
Fast YYYY-MM-DD format check before parsing with Date.
7. Credit card (basic)
/^\d{4}[\s-]?\d{4}[\s-]?\d{4}[\s-]?\d{4}$/
Accepts groups separated by spaces or hyphens. Always run Luhn on top.
8. Strong password
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\w]).{8,}$/
Requires lower, upper, digit, and a symbol, minimum 8 characters.
9. Remove extra whitespace
str.replace(/\s+/g, ' ').trim()
Collapses multiple spaces/tabs/newlines to a single space.
10. Extract numbers
str.match(/-?\d+(\.\d+)?/g)
Pulls every integer or decimal number out of a mixed string.
Practice these in ByteForge's Regex Tester — live highlighting makes it easy to see what each pattern matches.