PureDevTools

Regex Pattern Library

Searchable collection of 35+ common regex patterns with descriptions, examples, and a live test input

All processing happens in your browser. No data is sent to any server.

Category

Showing all 36 patterns

Email Address

Email & Web
/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/

Matches a valid email address per RFC 5321. Allows dots, plus signs, and special characters in the local part.

HTTP / HTTPS URL

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

Matches HTTP and HTTPS URLs including path, query string, and fragment.

Domain Name

Email & Web
/^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/

Matches a bare domain name with at least one dot and a valid TLD of 2+ characters.

US Phone Number

Phone & Numbers
/^\+?1?[-. ]?\(?([2-9][0-9]{2})\)?[-. ]?([2-9][0-9]{2})[-. ]?([0-9]{4})$/

Matches US phone numbers in various formats: with or without country code, parentheses, dashes, dots, or spaces.

International Phone (E.164)

Phone & Numbers
/^\+[1-9]\d{6,14}$/

Matches E.164 international phone numbers. Must start with + followed by 7–15 digits.

Integer

Phone & Numbers
/^-?[0-9]+$/

Matches a positive or negative whole number. No decimal point or whitespace allowed.

Decimal Number

Phone & Numbers
/^-?[0-9]+(\.[0-9]+)?$/

Matches integers and decimal numbers, including negatives.

Credit Card Number

Phone & Numbers
/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13}|6(?:011|5[0-9]{2})[0-9]{12})$/

Matches Visa (16 digits starting with 4), Mastercard (16 digits starting with 51–55), Amex (15 digits starting with 34/37), and Discover (16 digits starting with 6011 or 65). Digits only, no spaces or dashes.

ISO 8601 Date (YYYY-MM-DD)

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

Matches dates in ISO 8601 format (YYYY-MM-DD). Validates month (01–12) and day (01–31) ranges, but does not check for calendar validity (e.g., Feb 31).

US Date (MM/DD/YYYY)

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

Matches dates in US format MM/DD/YYYY with leading zeros. Validates month and day ranges.

24-Hour Time (HH:MM)

Date & Time
/^([01][0-9]|2[0-3]):[0-5][0-9]$/

Matches 24-hour clock times from 00:00 to 23:59 with mandatory leading zeros.

12-Hour Time (HH:MM AM/PM)

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

Matches 12-hour clock times with AM/PM suffix.

IPv4 Address

Network
/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/

Matches a valid IPv4 address where each octet is 0–255. Rejects leading zeros and values above 255.

IPv6 Address

Network
/^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$|^(([0-9a-fA-F]{1,4}:)*[0-9a-fA-F]{1,4})?::((([0-9a-fA-F]{1,4}:)*[0-9a-fA-F]{1,4})?)$/

Matches full and compressed IPv6 addresses. Handles the :: shorthand for consecutive zero groups.

MAC Address

Network
/^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/

Matches a MAC address in colon or hyphen notation (e.g., AA:BB:CC:DD:EE:FF or AA-BB-CC-DD-EE-FF).

Strong Password

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

Requires at least 8 characters with at least one uppercase letter, one lowercase letter, one digit, and one special character (@$!%*?&).

MD5 Hash

Security
/^[a-fA-F0-9]{32}$/

Matches a 32-character hexadecimal MD5 hash.

SHA-256 Hash

Security
/^[a-fA-F0-9]{64}$/

Matches a 64-character hexadecimal SHA-256 hash.

JWT Token

Security
/^[A-Za-z0-9_-]{2,}(?:\.[A-Za-z0-9_-]{2,}){2}$/

Matches a JSON Web Token (three Base64URL-encoded segments separated by dots).

Unix File Path

File & Path
/^(\/[^\0\/]+)+\/?$/

Matches a Unix-style absolute file path starting with /. Disallows null bytes.

Windows File Path

File & Path
/^[a-zA-Z]:\\(?:[^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*$/

Matches a Windows absolute file path with drive letter. Disallows characters invalid on Windows.

File Extension

File & Path
/^.+\.(jpg|jpeg|png|gif|webp|svg|pdf|doc|docx|xls|xlsx|zip|tar|gz|mp4|mp3|txt|csv|json|xml|html|css|js|ts)$/i

Matches filenames with common file extensions (images, documents, archives, audio/video, web, data). Case-insensitive.

HTML Tag

HTML & CSS
/<([a-zA-Z][a-zA-Z0-9]*)\b[^>]*>([^<]*)<\/\1>|<([a-zA-Z][a-zA-Z0-9]*)\b[^>]*\/?>/g

Matches HTML tags including self-closing tags. Note: a full HTML parser is always preferred over regex for HTML processing.

Hex Color Code

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

Matches 3-digit and 6-digit hexadecimal CSS color codes starting with #.

CSS Class/ID Selector

HTML & CSS
/^[.#][a-zA-Z_-][a-zA-Z0-9_-]*$/

Matches a single CSS class selector (starting with .) or ID selector (starting with #).

US Social Security Number

Identity
/^(?!219-09-9999|078-05-1120|000|666|9\d{2})\d{3}-(?!00)\d{2}-(?!0{4})\d{4}$/

Matches a US SSN in AAA-BB-CCCC format. Excludes invalid area codes (000, 666, 900–999) and known test/advertising SSNs.

US Zip Code

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

Matches a 5-digit US ZIP code or the extended ZIP+4 format (NNNNN-NNNN).

UK Postcode

Identity
/^[A-Z]{1,2}\d[A-Z\d]? ?\d[A-Z]{2}$/i

Matches a UK postcode in standard formats such as SW1A 2AA, EC1A 1BB, W1A 0AX, M1 1AE.

US Passport Number

Identity
/^[A-Z]{1,2}[0-9]{6,9}$/

Matches a US passport number format: 1–2 uppercase letters followed by 6–9 digits.

Username

Username & Social
/^[a-zA-Z0-9_-]{3,16}$/

Matches a username of 3–16 characters using letters, digits, underscores, and hyphens. Common rule on many platforms.

Twitter / X Handle

Username & Social
/^@?[A-Za-z0-9_]{1,15}$/

Matches a Twitter/X username (1–15 characters, letters, digits, underscores). The @ prefix is optional.

GitHub Username

Username & Social
/^[a-zA-Z0-9](?:[a-zA-Z0-9]|-(?=[a-zA-Z0-9])){0,38}$/

Matches a GitHub username: 1–39 characters, alphanumeric, no consecutive hyphens, no leading/trailing hyphen.

UUID (v4)

Text & Formatting
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i

Matches a UUID v4 in canonical hyphenated format. Validates the version (4) and variant (8, 9, a, or b) bits.

URL Slug

Text & Formatting
/^[a-z0-9]+(?:-[a-z0-9]+)*$/

Matches a URL-friendly slug: lowercase letters and digits, separated by single hyphens. No leading or trailing hyphens.

Semantic Version (SemVer)

Text & Formatting
/^(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-]+)*))?$/

Matches a semantic version string per semver.org spec. Supports pre-release labels and build metadata.

Latitude, Longitude

Text & Formatting
/^(-?(?:90(?:\.0+)?|(?:[0-8]?\d)(?:\.\d+)?)),\s*(-?(?:180(?:\.0+)?|(?:1[0-7]\d|\d{1,2})(?:\.\d+)?))$/

Matches a latitude/longitude pair separated by a comma. Latitude: -90 to 90. Longitude: -180 to 180.

You need a regex for email validation. You could write \S+@\S+ (too loose — matches foo@), or you could copy the RFC 5322 regex from Stack Overflow (200 characters, unreadable, and probably wrong). You need a tested, documented pattern with example matches and a live test input so you can verify it works with your data before pasting it into production.

Why This Library (Not the Regex Cheat Sheet or Tester)

PureDevTools has a Regex Cheat Sheet for syntax reference and a Regex Tester for testing custom expressions. This library provides 35+ pre-built, tested patterns for email, URL, phone, date, IP address, credit card, password strength, HTML tags, file paths, UUID, and more — each with a live test input and example matches. Use this library for common patterns; use the cheat sheet to learn syntax; use the tester for custom expressions.

What Is a Regular Expression?

A regular expression (regex or regexp) is a sequence of characters that defines a search pattern. Regex is built into virtually every programming language and text editor, and is the standard tool for:

How to Use These Patterns

Each pattern card shows the full regex in /pattern/flags notation. Click Copy to copy the pattern, or type in the Test it field to see a live match result.

JavaScript

const emailRegex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
emailRegex.test("user@example.com"); // true

// Named capture groups (ES2018+)
const dateRegex = /^(?<year>\d{4})-(?<month>0[1-9]|1[0-2])-(?<day>0[1-9]|[12]\d|3[01])$/;
const match = "2024-01-15".match(dateRegex);
console.log(match?.groups?.year); // "2024"

Python

import re

email_pattern = re.compile(
    r"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+"
    r"@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?"
    r"(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"
)
bool(email_pattern.match("user@example.com"))  # True

PHP

$pattern = '/^[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/';
preg_match($pattern, 'user@example.com'); // 1

Java

import java.util.regex.*;

Pattern p = Pattern.compile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@...$");
Matcher m = p.matcher("user@example.com");
boolean matches = m.matches(); // true

Pattern Categories

Email & Web

The Email Address pattern follows RFC 5321 and allows dots, plus signs, and special characters in the local part. It does not verify deliverability — for that, send a confirmation email.

The HTTP/HTTPS URL pattern matches complete web URLs including path, query string, and fragments. It requires the http:// or https:// scheme.

Phone & Numbers

Phone number formats vary widely by country. The US Phone Number pattern handles the most common US formats including parentheses, dashes, dots, and the +1 country code. For international numbers, use the E.164 pattern which requires the + prefix followed by 7–15 digits.

The Credit Card pattern matches the digit-only format (no spaces or dashes) for Visa, Mastercard, American Express, and Discover cards. Always use a dedicated payment library (Stripe, Braintree) for production validation — regex alone cannot validate a card’s actual validity or expiration.

Date & Time

The ISO 8601 Date pattern (YYYY-MM-DD) is the international standard and is the safest format to use in APIs and databases because it is unambiguous across locales. The US Date pattern (MM/DD/YYYY) is common in North American applications.

Note: these patterns validate the format and basic range of month/day values, but they cannot check for calendar validity (e.g., February 31 passes). For full date validation, parse with a date library.

Network

The IPv4 pattern validates all four octets to be in the range 0–255. The IPv6 pattern handles both fully-expanded (2001:0db8:...) and compressed (::1, fe80::1) forms. The MAC Address pattern accepts both colon (AA:BB:CC:DD:EE:FF) and hyphen (AA-BB-CC-DD-EE-FF) notation.

Security

The Strong Password pattern enforces a minimum set of requirements common in security policies: 8+ characters, at least one uppercase, one lowercase, one digit, and one special character. Adjust the special character set to match your policy.

The MD5 and SHA-256 hash patterns simply check for the correct hexadecimal length (32 and 64 characters, respectively). They do not validate the content of the hash.

The JWT Token pattern checks for the three-segment Base64URL structure. Use a dedicated JWT library (jsonwebtoken, jose) to verify the signature.

File & Path

The Unix File Path pattern requires an absolute path starting with /. It disallows null bytes and standalone slashes.

The Windows File Path pattern requires a drive letter and backslash notation. It rejects characters that are invalid on Windows (< > : " / | ? *).

The File Extension pattern covers the most common file types. Add extensions to the alternation group as needed for your use case.

HTML & CSS

Regex is a poor tool for parsing HTML in general — a full DOM parser (browser DOMParser, Python BeautifulSoup, etc.) is almost always the right choice. However, regex can be useful for simple, well-controlled cases like finding all <img> tags in a controlled template.

The Hex Color pattern matches both 3-digit (#FFF) and 6-digit (#FFFFFF) CSS hex colors.

Identity

The US SSN pattern matches the AAA-BB-CCCC format and excludes known invalid area codes (000, 666, 900–999) and a few widely-publicized advertising SSNs. Handle SSNs with care — they are sensitive PII and must be encrypted at rest.

The US ZIP Code pattern matches both the basic 5-digit form and the extended ZIP+4 form (NNNNN-NNNN).

The UK Postcode pattern covers the standard UK postcode formats used by Royal Mail.

Username & Social

The Username pattern (3–16 characters, alphanumeric with underscores and hyphens) is a common baseline used by many platforms. Always check your specific platform’s rules.

The Twitter/X Handle pattern matches 1–15 characters. Note that Twitter does not allow handles starting with a number in the current UI, but the API accepts them.

The GitHub Username pattern enforces GitHub’s rules: 1–39 alphanumeric characters, hyphens allowed but not consecutively or at the start/end.

Text & Formatting

The UUID v4 pattern validates the canonical hyphenated format and checks the version bit (the 4 in the third segment) and variant bits (the 8, 9, a, or b in the fourth segment).

The URL Slug pattern enforces the common convention for URL-friendly strings: lowercase, digits, and single hyphens only.

The Semantic Version pattern implements the full semver.org specification, including pre-release labels (1.0.0-alpha.1) and build metadata (1.0.0+build.1).

Regex Flags Reference

FlagMeaning
(none)Case-sensitive match, ^ and $ match start/end of string
iCase-insensitive match
gGlobal — find all matches, not just the first
mMultiline — ^ and $ match start/end of each line
sDotall — . matches newline characters

Common Regex Syntax Reference

SyntaxMeaning
.Any character except newline
\dDigit ([0-9])
\wWord character ([a-zA-Z0-9_])
\sWhitespace
^Start of string (or line with m flag)
$End of string (or line with m flag)
*0 or more
+1 or more
?0 or 1 (also makes quantifiers lazy)
{n,m}Between n and m repetitions
[abc]Character class
[^abc]Negated character class
(abc)Capturing group
(?:abc)Non-capturing group
(?=abc)Positive lookahead
(?!abc)Negative lookahead
a|bAlternation (a or b)

ReDoS Safety

ReDoS (Regular Expression Denial of Service) occurs when a poorly written regex takes exponential time to process certain crafted inputs. To stay safe:

  1. Limit input length before applying regex on the server side
  2. Avoid nested quantifiers on overlapping character sets (e.g., (a+)+)
  3. Use atomic groups or possessive quantifiers if your engine supports them
  4. Test with adversarial inputs — try inputs with many repeated characters before the failure point

The patterns in this library are designed for single-string validation and do not have known catastrophic backtracking issues when used as intended (i.e., testing a complete, length-limited input string).

Frequently Asked Questions

What is the difference between match() and test() in JavaScript? RegExp.prototype.test(str) returns a boolean (does the pattern match?). String.prototype.match(regex) returns the match array or null. For validation, use test(). For extraction, use match() or String.matchAll().

Why does my email regex reject valid addresses? Email validation is famously complex — the full RFC 5321 grammar allows unusual characters and quoted strings. Most teams use a simple pattern that covers 99.9% of real-world addresses and then confirm deliverability via a confirmation link.

Should I use ^ and $ in my patterns? Yes, for validation. Without them, /\d+/ matches 123 inside abc123xyz. With ^\d+$, you require the entire string to be digits. Always anchor validation patterns with ^ and $.

What is a non-capturing group (?:...)? A non-capturing group groups a subexpression without recording the matched text in the capture buffer. Use it for alternation or repetition when you do not need the matched content — it is slightly faster and reduces memory use compared to capturing groups.

Can I use these patterns across programming languages? Yes. JavaScript, Python, Java, PHP, Go, Ruby, and .NET all support PCRE-compatible regex with minor syntactic differences. The main adjustment is string escaping: JavaScript regex literals use \d, while Python uses raw strings (r'\d') or double backslashes ('\\d'). The logic of the patterns transfers directly.

Related Tools

More JavaScript Tools