GadgetForge

GadgetForge

Commonly Used Regular Expressions (Regex) Cheat Sheet


Commonly Used Regular Expressions Cheat Sheet

A practical collection of regex patterns for common validation and matching tasks. Test each pattern live using the linked regex tester.

1. Digits & Numbers

  • Whole Numbers
    /^\d+$/
    Test →

  • Decimal Numbers
    /^\d*\.\d+$/
    Test →

  • Whole + Decimal Numbers
    /^\d*(\.\d+)?$/
    Test →

  • Negative, Positive Whole + Decimal
    /^-?\d*(\.\d+)?$/
    Test →

  • Whole + Decimal + Fractions
    /[-]?[0-9]+[,.]?[0-9]*([\/][0-9]+[,.]?[0-9]*)*/
    Test →

2. Alphanumeric

  • Alphanumeric (no spaces)
    /^[a-zA-Z0-9]*$/
    Test →

  • Alphanumeric (with spaces)
    /^[a-zA-Z0-9 ]*$/
    Test →

3. Email Addresses

  • Common Email Format
    /^([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6})*$/
    Test →

  • More Flexible (uncommon emails)
    /^([a-z0-9_\.\+-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/
    Test →

4. Password Strength

  • Complex (≥8 chars, 1 lowercase, 1 uppercase, 1 number, 1 special)
    /(?=(.*[0-9]))(?=.*[\!@#$%^&*()\\[\]{}\\-_+=~|:;"'<>,./?])(?=.[a-z])(?=(.[A-Z]))(?=(.*)).{8,}/`
    Test →

  • Moderate (≥8 chars, 1 lowercase, 1 uppercase, 1 number)
    /(?=(.*[0-9]))((?=.*[A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z]))^.{8,}$/
    Test →

5. Username

  • Alphanumeric + _ - (3–16 characters)
    /^[a-z0-9_-]{3,16}$/
    Test →

6. URLs

  • With http(s) protocol
    /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/
    Test →

  • Protocol optional
    /(https?:\/\/)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/
    Test →

7. IP Addresses

  • IPv4
    /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/
    Test →

  • IPv6 (full pattern — very long)
    (See full pattern in original source)
    Test →

  • IPv4 or IPv6
    (Combined pattern — see source for complete regex)
    Test →

8. Dates

  • YYYY-MM-DD
    /([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/
    Test →

  • DD-MM-YYYY (with -, ., / separators)
    (See full pattern in source)
    Test →

  • DD-mmm-YYYY (month names)
    (See full pattern in source)
    Test →

9. Time Formats

  • HH:MM 12-hour (optional leading zero)
    /^(0?[1-9]|1[0-2]):[0-5][0-9]$/

  • HH:MM 12-hour with AM/PM
    /((1[0-2]|0?[1-9]):([0-5][0-9]) ?([AaPp][Mm]))/

  • HH:MM 24-hour (leading zero)
    /^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/

  • HH:MM 24-hour (optional leading zero)
    /^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/

  • HH:MM:SS 24-hour
    /(?:[01]\d|2[0123]):(?:[012345]\d):(?:[012345]\d)/

10. HTML & JavaScript

  • HTML Tags (with/without attributes)
    /<\/?[\w\s]*>|<.+[\W]>/
    Test →

  • Inline JS Event Handlers
    /\bon\w+=\S+(?=.*>)/
    Test →

11. Slug

  • URL-friendly slug
    /^[a-z0-9]+(?:-[a-z0-9]+)*$/
    Test →

12. Find Duplicates in String

  • Repeated words
    /(\b\w+\b)(?=.*\b\1\b)/
    Test →

13. Phone Numbers

  • International Phone Numbers (with optional country code / extension)
    /^(?:(?:\(?(?:00|\+)([1-4]\d\d|[1-9]\d?)\)?)?[\-\.\ \\\/]?)?((?:\(?\d{1,}\)?[\-\.\ \\\/]?){0,})(?:[\-\.\ \\\/]?(?:#|ext\.?|extension|x)[\-\.\ \\\/]?(\d+))?$/
    Test →

Important: Prefer using a proper phone validation library instead of regex when possible.

Additional Patterns

  • Zip / Postal Codes
    No universal regex exists. Use country-specific patterns: See list

  • Credit Card Numbers
    Common card patterns

  • Social Security Number (US SSN)
    /^((?!219-09-9999|078-05-1120)(?!666|000|9\d{2})\d{3}-(?!00)\d{2}-(?!0{4})\d{4})|((?!219 09 9999|078 05 1120)(?!666|000|9\d{2})\d{3} (?!00)\d{2} (?!0{4})\d{4})|((?!219099999|078051120)(?!666|000|9\d{2})\d{3}(?!00)\d{2}(?!0{4})\d{4})$/

  • Passport Number (basic pattern)
    /^[A-PR-WY][1-9]\d\s?\d{4}[1-9]$/

Useful References

Happy regexing! 🚀