Regex Patterns for Common Validations: Copy-Paste Ready Regular Expressions
Let's be honest: writing regular expressions from scratch is like solving a Rubik's cube blindfolded. You know it's theoretically possible, but why torture yourself when someone's already figured it out?
In this guide, I've compiled battle-tested regex patterns for the most common validation scenariosβemail addresses, phone numbers, URLs, passwords, credit cards, dates, and more. Each pattern comes with an explanation, examples, and edge cases to watch out for.
Bookmark this page. You'll come back to it more times than you'll want to admit.
Email Validation
π§ Standard Email Validation (99% of Cases)
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$
Matches:
- user@example.com β
- john.doe+filter@company.co.uk β
- test_email@sub.domain.org β
Rejects:
- @example.com β (no local part)
- user@.com β (domain starts with dot)
- user@domain β (no TLD)
Reality Check: There's no perfect email regex that catches 100% of valid emails per RFC 5322 without becoming impossibly complex. The pattern above works for 99% of real-world emails. For mission-critical validation, send a confirmation emailβit's the only way to truly verify an address works.
Stricter Email Pattern (No Special Characters)
^[a-zA-Z0-9]+@[a-zA-Z0-9]+\\.[a-zA-Z]{2,}$
Use this for systems where you want to avoid edge cases with dots, plus signs, or underscores.
Phone Number Validation
π± US Phone Number (10 digits)
^(\\+1)?[-.]?\\(?\\d{3}\\)?[-.]?\\d{3}[-.]?\\d{4}$
Matches:
- (555) 123-4567 β
- 555-123-4567 β
- 5551234567 β
- +1-555-123-4567 β
π International Phone Number (Flexible)
^\\+?[1-9]\\d{1,14}$
Based on E.164 standard. Allows 1-15 digits with optional leading +.
Matches:
- +14155552671 β
- +442071234567 β (UK)
- +8613812345678 β (China)
Pro Tip: For production systems, use a library like libphonenumber (Google's phone number library) instead of regex. Phone number validation is complex with country-specific rules that regex can't fully handle.
URL Validation
π HTTP/HTTPS URLs
^https?:\\/\\/(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b([-a-zA-Z0-9()@:%_\\+.~#?&\\/\\/=]*)$
Matches:
- https://example.com β
- http://sub.example.com/path?query=1 β
- https://example.com:8080/api β
Rejects:
- example.com β (no protocol)
- ftp://example.com β (not HTTP/HTTPS)
π Any URL (Including FTP, File, etc.)
^[a-zA-Z][a-zA-Z0-9+.-]*:\\/\\/[^\\s]+$
More permissiveβallows any protocol.
π§ͺ Test Your Regex Patterns Live
Use our free Regex Tester with real-time highlighting, group extraction, and pattern explanation. Test against multiple strings simultaneously and catch edge cases before deployment.
Open Regex Tester βPassword Validation
π Strong Password (8+ chars, mixed case, number, symbol)
^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*?&])[A-Za-z\\d@$!%*?&]{8,}$
Requirements:
- At least 8 characters
- At least one lowercase letter
- At least one uppercase letter
- At least one digit
- At least one special character (@$!%*?&)
Matches:
- P@ssw0rd β
- Secure123! β
Rejects:
- password β (no uppercase, digit, symbol)
- Pass1! β (too short)
π‘οΈ Medium Password (8+ chars, letter + number)
^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d@$!%*?&]{8,}$
Less strictβrequires only letters and numbers.
Credit Card Validation
π³ Credit Card Number (13-19 digits)
^\\d{13,19}$
Specific Card Types:
Visa: ^4[0-9]{12}(?:[0-9]{3})?$
Mastercard: ^5[1-5][0-9]{14}$
Amex: ^3[47][0-9]{13}$
Discover: ^6(?:011|5[0-9]{2})[0-9]{12}$
β οΈ Important: Regex only validates format, not validity. Use the Luhn algorithm to verify the checksum digit. Never log or store full credit card numbersβthat violates PCI-DSS compliance!
Date Validation
π ISO 8601 Date (YYYY-MM-DD)
^\\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\\d|3[01])$
Matches:
- 2026-01-12 β
- 2026-12-31 β
Rejects:
πΊπΈ US Date (MM/DD/YYYY)
^(?:0[1-9]|1[0-2])\\/(?:0[1-9]|[12]\\d|3[01])\\/\\d{4}$
Matches: 01/12/2026, 12/31/2026
IPv4 Address
π IPv4 Address
^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$
Matches:
- 192.168.1.1 β
- 10.0.0.255 β
- 8.8.8.8 β
Rejects:
- 256.1.1.1 β (octet > 255)
- 192.168.1 β (incomplete)
Username Validation
π€ Alphanumeric Username (3-16 chars)
^[a-zA-Z0-9_-]{3,16}$
Allows letters, numbers, underscores, hyphens. Length: 3-16 characters.
Matches:
- john_doe β
- user-123 β
- CoolUser β
Rejects:
- ab β (too short)
- user@name β (@ not allowed)
HTML Tags
π·οΈ HTML Tag
<\\/?[a-z][a-z0-9]*[^<>]*>
Matches opening and closing HTML tags.
Matches:
- <div> β
- <img src="test.jpg" /> β
- </div> β
β οΈ Don't Parse HTML with Regex: This pattern is for basic tag detection only. For actual HTML parsing, use a proper DOM parser like DOMParser (browser) or cheerio/jsdom (Node.js). Regex can't handle nested tags or edge cases.
Hex Color Code
π¨ Hex Color (3 or 6 digits)
^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$
Matches:
- #FF5733 β
- #F53 β
- FF5733 β (without #)
Slug (URL-Friendly String)
π URL Slug
^[a-z0-9]+(?:-[a-z0-9]+)*$
Lowercase letters, numbers, hyphens. No leading/trailing hyphens.
Matches:
- hello-world β
- my-article-2026 β
Rejects:
- -hello β (leading hyphen)
- hello- β (trailing hyphen)
- Hello-World β (uppercase)
Social Security Number (US)
π SSN Format
^(?!000|666)[0-9]{3}-(?!00)[0-9]{2}-(?!0000)[0-9]{4}$
Validates XXX-XX-XXXX format with exclusions (no 000, 666 area codes).
Matches: 123-45-6789 β
Rejects: 000-45-6789 β, 666-45-6789 β
File Extensions
π Image Files
\\.(jpg|jpeg|png|gif|bmp|svg|webp)$
Matches: photo.jpg, image.png, icon.svg
π Document Files
\\.(pdf|doc|docx|txt|rtf|odt)$
Matches: report.pdf, document.docx
Time Validation
β° 24-Hour Time (HH:MM)
^(?:[01]\\d|2[0-3]):[0-5]\\d$
Matches: 00:00, 13:45, 23:59 β
Rejects: 24:00 β, 13:60 β
π 12-Hour Time (HH:MM AM/PM)
^(?:0?[1-9]|1[0-2]):[0-5]\\d\\s?[AP]M$
Matches: 1:30 PM, 12:00 AM, 09:45 AM β
Best Practices for Regex Validation
1. Always Test Edge Cases
Your regex might work for happy-path inputs but fail on:
- Empty strings
- Very long inputs
- Unicode characters
- Whitespace (leading/trailing)
2. Use Anchors (^ and $)
Without anchors, your pattern might match a substring instead of the whole string:
// WITHOUT anchors (WRONG)
/\\d{3}/ matches "abc123def" β
(finds "123")
// WITH anchors (CORRECT)
/^\\d{3}$/ rejects "abc123def" β (entire string must be 3 digits)
3. Escape Special Characters
These characters have special meaning in regex and must be escaped: . * + ? ^ $ { } ( ) | [ ] \\
// Match literal dot
/\\./ instead of /./
// Match literal question mark
/\\?/ instead of /?/
4. Use Non-Capturing Groups When Possible
// Capturing group (stores in memory)
/(foo|bar)/
// Non-capturing group (better performance)
/(?:foo|bar)/
5. Consider Performance for Long Strings
Avoid catastrophic backtracking with nested quantifiers:
// BAD (can hang on long strings)
/(a+)+$/
// GOOD (linear time)
/a+$/
Conclusion: Regex is a Tool, Not a Religion
Regular expressions are powerful, but they're not always the right solution:
- β Use regex for: Format validation, pattern matching, text extraction
- β Don't use regex for: HTML parsing, email verification (use SMTP), complex business logic
When in doubt, combine regex with other validation:
// Format check (regex)
const isValidFormat = /^\\d{13,19}$/.test(cardNumber);
// Checksum validation (Luhn algorithm)
const isValidChecksum = luhnCheck(cardNumber);
return isValidFormat && isValidChecksum;
π§ͺ Test Your Patterns Now
Use our free Regex Tester to validate these patterns against your data. Features real-time highlighting, group extraction, and multi-string testing. All processing happens locally in your browser.
Open Regex Lab β