GadgetForge

Blog

GadgetForge

BlogCheatsheets

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

2. Alphanumeric

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

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! ๐Ÿš€