Regex Patterns for Common Validations: Copy-Paste Ready Regular Expressions

Published on January 12, 2026 | 12 min read | By GetDevUtils Team

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:

  • 2026-13-01 ❌ (month > 12)
  • 2026-01-32 ❌ (day > 31)
  • πŸ‡ΊπŸ‡Έ 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:

    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:

    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 β†’

    ← Back to Blog | GetDevUtils Home