Regex Cheat Sheet & Common Patterns
Quick reference guide for regular expressions. Copy-paste ready patterns for emails, URLs, phone numbers, dates, and more.
📚 Quick Regex Basics
| Syntax | Description | Example |
|---|---|---|
| . | Any single character except newline | a.c matches "abc", "a1c" |
| ^ | Start of string | ^Hello matches "Hello world" |
| $ | End of string | world$ matches "Hello world" |
| * | 0 or more of preceding element | ab*c matches "ac", "abc", "abbc" |
| + | 1 or more of preceding element | ab+c matches "abc", "abbc" |
| ? | 0 or 1 of preceding element | colou?r matches "color", "colour" |
| {n} | Exactly n occurrences | \d{3} matches "123" |
| {n,m} | Between n and m occurrences | \d{2,4} matches "12", "123", "1234" |
| [abc] | Any character in brackets | [aeiou] matches any vowel |
| [^abc] | Any character NOT in brackets | [^0-9] matches non-digits |
| | | OR operator | cat|dog matches "cat" or "dog" |
| () | Capture group | (\d{3})-(\d{2}) captures groups |
| \d | Any digit (0-9) | \d+ matches "123" |
| \w | Word character (a-z, A-Z, 0-9, _) | \w+ matches "hello" |
| \s | Whitespace character | \s+ matches spaces/tabs |
| \b | Word boundary | \bcat\b matches "cat" not "category" |
Common Validation Patterns
Email Address
Matches standard email addresses with various TLDs and subdomains.
URL (HTTP/HTTPS)
Matches HTTP and HTTPS URLs with optional www prefix.
US Phone Number
Matches US phone numbers in various formats including country code.
IPv4 Address
Validates IPv4 addresses (0.0.0.0 to 255.255.255.255).
Strong Password
At least 8 characters with uppercase, lowercase, number, and special character.
Credit Card Number
Matches Visa (16 digits), Mastercard (16 digits), and Amex (15 digits).
Date & Time Patterns
Date (YYYY-MM-DD)
ISO 8601 date format. Validates month and day ranges.
Date (MM/DD/YYYY)
US date format with forward slashes.
Time (24-hour)
24-hour time format (HH:MM).
Time (12-hour with AM/PM)
12-hour time format with AM/PM.
Text & Content Patterns
Username (Alphanumeric + Underscore)
3-16 characters, letters, numbers, and underscores only.
Hex Color Code
Matches 3 or 6 digit hex color codes with # prefix.
HTML Tag
Matches HTML opening and closing tags.
Social Security Number (SSN)
US Social Security Number format (XXX-XX-XXXX).
ZIP Code (US)
5-digit or ZIP+4 format.
Slug (URL-friendly)
Lowercase letters, numbers, and hyphens for SEO-friendly URLs.
Programming Patterns
JavaScript Variable Name
Valid JavaScript variable/function names. Starts with letter, $, or _.
File Extension
Extracts file extension from filename.
Semantic Version
Validates semantic versioning (MAJOR.MINOR.PATCH).
⚡ Regex Performance Tips
- Be specific: Use
\dinstead of[0-9]for better performance - Avoid greedy quantifiers: Use
.*?(lazy) instead of.*(greedy) when possible - Use anchors:
^and$improve performance by limiting search scope - Avoid catastrophic backtracking: Be careful with nested quantifiers like
(a+)+ - Test thoroughly: Use tools like Regex Tester to validate patterns
🔧 Online Regex Tools
- TextPlus Regex Tester - Test patterns right here
- Regex101.com - Detailed explanations and debugger
- RegExr.com - Visual regex builder with cheat sheet
- RegexPal.com - Simple live regex testing