Ready-to-use regex patterns, with examples
Most regular expressions you need have been written many times before: an email check in a sign-up form, a postal code field, a UUID in an API route. This library collects 45 regex patterns for those everyday jobs. Every pattern lists values it matches, values it rejects and the cases it deliberately does not check, and all of those examples are covered by automated tests in JavaScript, with the generated PHP, Python and Java code tested against the same examples.
Email regex: which one should you use?
Email is the pattern people look for most, and there is no single right answer. The library offers three, from loose to strict:
- Simple:
^[^\s@]+@[^\s@]+\.[^\s@]+$accepts anything shaped like name@domain.tld. Use it for a friendly first check while someone types. - HTML input type="email": the rule browsers apply to email form fields. It accepts
user@localhostbecause intranet addresses need no dot, and it does not object toa..b@example.com. Use it when your server should agree exactly with the browser. - Practical RFC 5322 subset: a local part without leading, trailing or double dots, a domain with at least one dot, a letters-only top-level domain, and two lookaheads that enforce the 64-character local part and 254-character total limits.
Why full RFC validation by regex is impractical
The address grammar in RFC 5322 allows quoted local parts with spaces ("john doe"@example.com), comments in brackets, IP-address domains such as user@[192.0.2.1] and obsolete forms kept for old mail systems. A regex that covers all of it runs to thousands of characters, is hard to review, and still cannot tell whether the mailbox exists. Use a sensible pattern to catch typos, then confirm the address by sending a verification email. To check a whole list at once, the email validator explains why each address fails.
What is in the library
| Category | Patterns |
|---|---|
| Phone numbers | E.164, US and Canada (NANP), international loose, Indian mobile |
| Postal codes | US ZIP and ZIP+4, UK postcode, Canadian postal code, Indian PIN |
| Dates and times | YYYY-MM-DD, ISO 8601 date-time, DD/MM/YYYY, 24-hour time |
| Numbers | integer, decimal, positive number, thousands separators, US dollar amounts |
| Identifiers | UUID, UUID v4, slug, username, hex colour, MAC address, semantic version, ISBN-10, ISBN-13, card number format |
| Web | URL, URLs inside text, IPv4, IPv6, hostname, domain, HTML tag |
| Text and passwords | alphanumeric, letters only, names in any alphabet, no special characters, trimming, repeated words, password rules |
A regex can only check the shape of a phone number. To check that a number is possible for its country and format it, use the phone number validator.
Whole values or matches inside text
Patterns marked “validates a whole value” start with ^ and end with $, so the entire input must match: ^\d{5}$ accepts 90210 but not zip 90210. Patterns marked “finds text inside a string” have no anchors and use the g flag, like the URL finder that pulls every link out of a paragraph and leaves the full stop at the end of a sentence outside the match.
Copying a pattern into your language
Patterns are written in JavaScript syntax, and the copy buttons convert them:
- JavaScript: a regex literal with slashes escaped, e.g.
/^\d{5}(?:-\d{4})?$/. - Python:
re.compile(r'…')with flags such asre.IGNORECASE. Patterns that use\p{…}need the third-partyregexmodule, and the copied code says so. - PHP: a single-quoted string with
/delimiters forpreg_match(), including theiorumodifier when the pattern needs it. - Java:
Pattern.compile("…")with every backslash doubled and flags such asPattern.CASE_INSENSITIVE.
One difference catches many people: in Python, PHP and Java, $ also matches just before a final line break, so "90210\n" passes ^\d{5}$. Use re.fullmatch() in Python or \z in PHP and Java, or trim the input first. Go's regexp package has no lookarounds or back-references, and the flavour notes say which patterns need changes there. The regex cheat sheet compares the main flavours feature by feature.
Know what a pattern does not check
A regex checks form, not meaning. The ISO date pattern accepts 2023-02-31, the card number pattern does not run the Luhn checksum, and the ISBN patterns do not verify the check digit. Each pattern lists those gaps under “What it does not validate”, with examples that still match, so you know which check to add in code. To take a pattern further, for example with a replacement or capture groups, paste it into the regex tester. The test boxes on this page run in your browser.