🧩 Regex Cheatsheet & Builder
Click patterns to insert them into the builder. Test your regex live against sample text.
12 Regex Patterns Every Developer Should Have Memorised (and How to Build Your Own)
Regex is one of those skills that separates developers who spend ten minutes on a text-parsing problem from those who spend two hours. The patterns themselves are not complicated — they follow a surprisingly small set of rules. What makes regex feel hard is the sheer density of symbols packed into one line. This guide cuts through that noise. You will get the most useful patterns ready to copy, an explanation of how each one actually works, and a mental model for building new ones from scratch.
1. The Email Pattern That Actually Works (Mostly)
The real RFC 5321 specification for valid email addresses is notoriously bizarre — technically user+"quoted string"@[127.0.0.1] is valid. For practical web validation, this pattern covers 99% of real addresses:
[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}
Breaking it down: the first character class [a-zA-Z0-9._%+\-]+ matches one or more alphanumeric characters plus the symbols commonly found in the local part. The literal @ divides it. The domain portion matches a hostname with dots, and \.[a-zA-Z]{2,} requires a TLD of at least two letters. It handles .co.uk and .photography equally well.
2. IPv4 Address With Range Validation
A naive pattern like \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} will match 999.999.999.999. The correct pattern validates each octet's numeric range:
(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)
The alternation inside each octet group handles three cases: 250–255, 200–249, and 0–199. This is a great example of using alternation (|) to encode numeric constraints that character classes alone cannot express.
3. ISO Date (YYYY-MM-DD) With Month Validation
\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])
The month group (?:0[1-9]|1[0-2]) prevents month 13 or 00. The day group handles days 01–09, 10–29, and 30–31. Note that this cannot validate February 30th — that requires logic beyond regex, which is worth knowing so you do not waste time trying.
4. URL Matching — Greedy Enough to Be Useful
https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9]{2,6}(?:\/[^\s]*)?
The s? makes https and http both valid. The optional (?:www\.)? handles subdomains gracefully. The path segment (?:\/[^\s]*)? uses a negative class — "everything that is not whitespace" — which is far more practical than trying to enumerate valid URL characters.
5. US Phone Number — Multiple Formats at Once
(?:\+?1[\s\-.])??\(?\d{3}\)?[\s\-.]?\d{3}[\s\-.]?\d{4}
Users enter phone numbers in a dozen different ways: (555) 123-4567, 555.123.4567, +1-555-123-4567. This pattern uses character classes like [\s\-.] to accept any reasonable separator and wraps optional parts in (?:...)?. The trick is to be permissive in the separator position but strict about digit counts.
6. Hex Color Codes — Both 3 and 6 Digit Forms
#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})\b
The alternation tries 6 digits first, then 3. The \b word boundary prevents matching #ffffff1. Adding the i flag lets you drop the A-F from the character class, simplifying it to #(?:[0-9a-f]{3}|[0-9a-f]{6})\b.
7. JWT Token Structure
[A-Za-z0-9_\-]+\.[A-Za-z0-9_\-]+\.[A-Za-z0-9_\-]+
A JWT is always three Base64url-encoded segments separated by dots. This pattern validates the structure — three non-empty segments using the Base64url alphabet (which uses - and _ instead of + and /). It will not verify the signature cryptographically, but it will catch malformed tokens before you bother decoding them.
8. Lookaheads — The Regex Superpower Most Developers Skip
Lookaheads match a position, not characters. They do not consume input. This makes them invaluable for complex validations like password rules:
^(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%]).{8,}$
Each (?=.*[A-Z]) scans forward from the current position asking "does this string contain an uppercase letter somewhere?" The three lookaheads all run from the same starting position (the start anchor ^), and then .{8,} actually consumes the characters. You can stack as many lookaheads as you need without changing what gets matched.
9. Named Capturing Groups — Readable Extraction
Instead of referencing groups by number (\1, \2), name them:
(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})
In JavaScript you access them via match.groups.year. This makes regex that parses structured data dramatically more maintainable, especially when groups are reordered later.
10. Non-Greedy Quantifiers Save You From Matching Too Much
Consider extracting content between HTML tags. Greedy <b>(.*)</b> on <b>bold</b> text <b>more</b> matches from the first <b> to the last </b>. Lazy <b>(.*?)</b> correctly matches each pair. The ? after a quantifier changes its behaviour from "match as much as possible" to "match as little as possible".
11. The Slug Pattern — URL-Safe Strings
^[a-z0-9]+(?:-[a-z0-9]+)*$
This enforces the common CMS convention: lowercase letters and numbers, with hyphens as separators but never at the start or end, never doubled. The non-capturing group (?:-[a-z0-9]+)* allows zero or more hyphenated segments after the first word.
12. Backreferences for Duplicate Detection
\b(\w+)\s+\1\b
The group (\w+) captures a word. \1 matches the exact same text again. This classic pattern finds repeated words like "the the" in text. Backreferences can also validate matching HTML tags: <([a-z]+)[^>]*>.*?<\/\1> ensures the closing tag matches the opening one.
Building Your Own Patterns: A Mental Model
Start from the structure, not the characters. Ask: what are the distinct sections of what I'm matching? Define each section as either a fixed literal, a character class, or a quantified group. Then add anchors (^ and $) only if you need the entire string to match rather than a substring. Use non-capturing groups (?:...) by default and only upgrade to capturing groups when you actually need to extract a value. Test with both valid and invalid inputs — a regex that accepts everything it should is only half done.
The patterns above cover the vast majority of practical validation and extraction tasks. With the builder above, you can click any building block, combine patterns, and test instantly against real input — which is the fastest way to develop an intuition that no amount of reading can fully replace.