Regular expressions are one of the most powerful tools in a developer's arsenal — and one of the most intimidating. This cheat sheet provides 20 battle-tested patterns you can copy, paste, and adapt for your projects.
Email Validation
The simple pattern: ^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,}$
This handles 99% of real email addresses. For strict RFC 5322 compliance, the regex becomes absurdly complex — but in practice, the simple version is more useful because the best validation is sending a confirmation email.
URL Matching
https?:\/\/[\w\-.]+(:\d+)?(\/[\w\-./?%&=]*)?
Matches HTTP and HTTPS URLs with optional port numbers and paths.
IP Address (IPv4)
\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b
Validates that each octet is between 0 and 255.
Phone Numbers (US)
\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}
Matches (555) 123-4567, 555-123-4567, 555.123.4567, and 5551234567.
Date (YYYY-MM-DD)
\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])
Validates ISO 8601 date format with basic month/day range checking.
Hex Color Code
#([0-9a-fA-F]{3}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})\b
Matches #RGB, #RRGGBB, and #RRGGBBAA formats.
Password Strength
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
Requires minimum 8 characters with at least one uppercase, lowercase, digit, and special character.
More Essential Patterns
Whitespace trimming: ^\s+|\s+$ — Duplicate spaces: \s{2,} — HTML tags: <[^>]+> — Numbers with commas: \d{1,3}(,\d{3})*(\.\d+)?
Test and debug your regex patterns with our interactive Regex Tester — with real-time matching, group highlighting, and explanation of pattern components.