20 Regex Patterns Every Developer Should Bookmark
A practical cheat sheet of the most useful regular expressions — email, URL, phone, IP, dates, and more. Copy, paste, and use.
Regular expressions are one of those tools that feel magical when they work and absolutely maddening when they don't. I've been collecting useful patterns for years, and these 20 are the ones I reach for again and again.
Bookmark this page. You'll come back to it.
The Basics You'll Forget
Before the patterns, a quick refresher on the pieces that trip people up:
.matches any character (except newline)*means zero or more,+means one or more,?means zero or one\dis a digit,\wis a word character,\sis whitespace^is start of string,$is end of string()groups,[]defines a character class,{}sets quantity
The 20 Patterns
1. Email Address
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
Covers most real-world emails. Not RFC 5322 compliant (almost nothing is), but handles 99% of valid addresses.
2. URL (HTTP/HTTPS)
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)
Matches both HTTP and HTTPS URLs with paths and query strings.
3. Phone Number (International)
^\+?[1-9]\d{1,14}$
E.164 format — works internationally. For US-specific: ^\(?[2-9]\d{2}\)?[-.\s]?\d{3}[-.\s]?\d{4}$
4. IPv4 Address
^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$
Validates each octet is 0-255. Many simpler patterns accept invalid addresses like 999.999.999.999.
5. IPv6 Address
^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$
Basic full-form IPv6. Doesn't cover shorthand notation (::), but handles the most common format.
6. Date (YYYY-MM-DD)
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$
ISO 8601 date format. Validates month and day ranges but doesn't check for impossible dates like Feb 31.
7. Time (HH:MM, 24-hour)
^([01]\d|2[0-3]):([0-5]\d)$
24-hour time format, 00:00 to 23:59.
8. Hex Color Code
^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$
Matches both 3-digit and 6-digit hex colors: #fff, #1a2b3c.
9. Strong Password
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
At least 8 characters with uppercase, lowercase, number, and special character. Adjust the minimum length and allowed characters as needed.
10. Username
^[a-zA-Z0-9_-]{3,20}$
Alphanumeric with underscores and hyphens, 3-20 characters.
11. HTML Tag
<\/?[\w\s]*>|<.+[\W]>
Matches opening and closing HTML tags. Don't use regex to parse HTML for real work — use a proper parser. But for quick searches, this works.
12. Whitespace Trimming
^\s+|\s+$
Matches leading and trailing whitespace. Useful for cleanup operations.
13. Duplicate Words
\b(\w+)\s+\1\b
Catches repeated words like "the the" or "is is". Great for proofreading.
14. File Extension
\.([a-zA-Z0-9]+)$
Extracts the file extension. Group 1 gives you just the extension without the dot.
15. Slug (URL-friendly string)
^[a-z0-9]+(?:-[a-z0-9]+)*$
Validates URL slugs: lowercase letters, numbers, and hyphens. No consecutive hyphens, doesn't start or end with one.
16. Credit Card Number (Basic)
^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13})$
Detects Visa, MasterCard, and American Express patterns. This is for format validation only — always use a payment processor for real validation.
17. UUID v4
^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$
Validates UUID version 4 format with the correct version and variant bits.
18. JSON String Value
"(?:[^"\\]|\\.)*"
Matches a properly escaped JSON string value, handling backslash escapes correctly.
19. CSS Property
([a-z-]+)\s*:\s*([^;]+);
Extracts CSS property-value pairs. Group 1 is the property name, group 2 is the value.
20. Markdown Link
\[([^\]]+)\]\(([^)]+)\)
Matches [text](url) links. Group 1 is the link text, group 2 is the URL.
Tips for Working with Regex
Start simple and build up. Don't try to write the perfect pattern in one go. Start with the simplest version that works, then add edge case handling.
Use a tester. Always test your patterns with real data before deploying them. Edge cases will surprise you.
Add comments. Most regex engines support verbose mode where you can add comments and whitespace for readability.
Know when not to use regex. Parsing HTML, validating complex dates, or handling deeply nested structures — use proper parsers for those.
You can also use text diff to verify your regex replacements produced the right output, or character counter to check the length of matched strings.
Regex is a skill that improves with practice. You don't need to memorize every syntax rule — you need to know enough to read patterns and modify them for your needs. Keep this cheat sheet handy, and you'll spend less time on Stack Overflow and more time actually building things.