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

^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$

Matches standard email addresses with various TLDs and subdomains.

✅ Matches:
user@example.com, john.doe@company.co.uk, test+filter@mail.org
❌ Doesn't match:
invalid@, @example.com, user@domain

URL (HTTP/HTTPS)

https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)

Matches HTTP and HTTPS URLs with optional www prefix.

✅ Matches:
https://example.com, http://www.site.org/path?query=value

US Phone Number

^\+?1?[-.\s]?\(?([0-9]{3})\)?[-.\s]?([0-9]{3})[-.\s]?([0-9]{4})$

Matches US phone numbers in various formats including country code.

✅ Matches:
(555) 123-4567, 555-123-4567, +1 555 123 4567, 5551234567

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]?)$

Validates IPv4 addresses (0.0.0.0 to 255.255.255.255).

✅ Matches:
192.168.1.1, 10.0.0.1, 255.255.255.0

Strong Password

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$

At least 8 characters with uppercase, lowercase, number, and special character.

✅ Matches:
MyPass123!, Secure@2024, Valid$Pass1

Credit Card Number

^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13})$

Matches Visa (16 digits), Mastercard (16 digits), and Amex (15 digits).

Note:
This validates format only, not actual card validity. Remove spaces/dashes before validation.

Date & Time Patterns

Date (YYYY-MM-DD)

^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$

ISO 8601 date format. Validates month and day ranges.

✅ Matches:
2024-01-15, 2024-12-31, 2023-06-01

Date (MM/DD/YYYY)

^(0[1-9]|1[0-2])\/(0[1-9]|[12][0-9]|3[01])\/\d{4}$

US date format with forward slashes.

✅ Matches:
01/15/2024, 12/31/2023, 06/01/2024

Time (24-hour)

^([01]?[0-9]|2[0-3]):[0-5][0-9]$

24-hour time format (HH:MM).

✅ Matches:
09:30, 14:45, 23:59, 00:00

Time (12-hour with AM/PM)

^(0?[1-9]|1[0-2]):[0-5][0-9]\s?(AM|PM|am|pm)$

12-hour time format with AM/PM.

✅ Matches:
9:30 AM, 02:45 PM, 11:59 pm

Text & Content Patterns

Username (Alphanumeric + Underscore)

^[a-zA-Z0-9_]{3,16}$

3-16 characters, letters, numbers, and underscores only.

✅ Matches:
john_doe, User123, test_user_1

Hex Color Code

^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$

Matches 3 or 6 digit hex color codes with # prefix.

✅ Matches:
#FFF, #000000, #3b82f6, #abc

HTML Tag

<\/?[\w\s]*>|<.+[\W]>

Matches HTML opening and closing tags.

✅ Matches:
<div>, </p>, <a href="url">, <br />

Social Security Number (SSN)

^\d{3}-\d{2}-\d{4}$

US Social Security Number format (XXX-XX-XXXX).

✅ Matches:
123-45-6789, 987-65-4321
⚠️ Security:
Never store or transmit SSNs in plain text!

ZIP Code (US)

^\d{5}(-\d{4})?$

5-digit or ZIP+4 format.

✅ Matches:
12345, 90210-1234

Slug (URL-friendly)

^[a-z0-9]+(?:-[a-z0-9]+)*$

Lowercase letters, numbers, and hyphens for SEO-friendly URLs.

✅ Matches:
my-blog-post, product-123, hello-world-2024

Programming Patterns

JavaScript Variable Name

^[a-zA-Z_$][a-zA-Z0-9_$]*$

Valid JavaScript variable/function names. Starts with letter, $, or _.

✅ Matches:
myVar, _private, $jquery, userName123

File Extension

\.[a-zA-Z0-9]+$

Extracts file extension from filename.

✅ Matches:
.jpg, .html, .tar.gz (extracts .gz)

Semantic Version

^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$

Validates semantic versioning (MAJOR.MINOR.PATCH).

✅ Matches:
1.0.0, 2.1.3, 1.0.0-alpha, 1.0.0-beta.1+build.123

⚡ Regex Performance Tips

  • Be specific: Use \d instead 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

← Back to all references