Top 20 Common Regex Patterns Cheat Sheet (Email, URL, IP, Phone, UUID)
A curated, production-ready regex cheat sheet with syntax breakdowns, regex flags, character classes, and ReDoS safety tips for web developers.
Top 20 Production Regex Patterns
Regular expressions are an essential tool for string manipulation, data validation, and text extraction. Below is a battle-tested reference table of standard regex patterns optimized for accuracy, cross-engine compatibility, and execution speed.
| Pattern Name | Regular Expression | Matching Example | Notes |
|---|---|---|---|
| Pragmatic Email | ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ | dev@cosharex.com | Recommended for web form input |
| RFC 5322 Email | ^[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])?)*$ | john.doe+tag@sub.domain.org | Strict standard compliance |
| HTTP/HTTPS URL | ^https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&\/=]*)$ | https://cosharex.com/tools?id=42 | Supports query params and hashes |
| IPv4 Address | ^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$ | 192.168.1.1 | Validates 0-255 octet ranges |
| IPv6 Address | ^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$ | 2001:0db8:85a3:0000:0000:8a2e:0370:7334 | Full 8-group hexadecimal |
| UUID v4 | ^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$ | f47ac10b-58cc-4372-a567-0e02b2c3d479 | Enforces version 4 and variant 1 |
| ISO 8601 Date | ^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])T(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d(?:\.\d+)?(?:Z|[+-](?:[01]\d|2[0-3]):[0-5]\d)$ | 2026-09-09T14:30:00.000Z | Full ISO date-time with timezone |
| Semantic Version (SemVer) | ^(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-]+)*))?$ | v2.1.0-beta.1+build.428 | Official semver.org specification |
| URL Slug | ^[a-z0-9]+(?:-[a-z0-9]+)*$ | how-to-write-regex-2026 | Lowercase alphanumeric with single dashes |
| E.164 Phone Number | ^\+[1-9]\d{1,14}$ | +14155552671 | International ITU standard format |
1. Web & Identity Validation (Email, URL, Slugs)
When validating user input in web applications, balance strict specification conformance with user experience. For example, the pragmatic email regex ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ catches 99.9% of common typing mistakes without rejecting valid addresses that use plus-addressing (user+newsletter@domain.com).
// Pragmatic Email Validation in TypeScript
export function isValidEmail(email: string): boolean {
// Disallow strings over 254 characters (RFC 5321 limit)
if (!email || email.length > 254) return false;
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
return emailRegex.test(email.trim());
}
// URL Slug Validator
export function isValidSlug(slug: string): boolean {
const slugRegex = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
return slugRegex.test(slug);
}2. Networking & Infrastructure (IPv4, IPv6, MAC, Ports)
Validating network addresses requires verifying numeric bounds, not just digit counts. The standard naive regex \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} erroneously accepts 999.999.999.999. The production IPv4 regex below explicitly restricts each octet to the 0-255 range:
Octet breakdown:
25[0-5] -> Matches 250 - 255
2[0-4][0-9] -> Matches 200 - 249
[01]?[0-9][0-9]? -> Matches 0 - 199
Combined IPv4 Pattern:
^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$
Port Number (1 - 65535):
^([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$3. Identifiers & Formats (UUID, ISO 8601, SemVer)
Structured identifiers frequently embed version bits and timestamp components. The UUID v4 pattern checks for the version digit 4 in position 13 and the RFC 4122 variant bits (8, 9, a, or b) in position 17:
import re
UUID_V4_REGEX = re.compile(
r'^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$',
re.IGNORECASE
)
def validate_uuid_v4(val: str) -> bool:
return bool(UUID_V4_REGEX.match(val))Regex Engine Flags Reference
Regex flags modify the matching execution behavior across the search space:
| Flag | Name | Description & Effect |
|---|---|---|
| g | Global | Finds all matches across the string rather than stopping at the first occurrence. |
| i | Ignore Case | Enables case-insensitive matching across character ranges (e.g. `[a-z]` matches `[A-Z]`). |
| m | Multiline | Changes `^` and `$` to match the start/end of each individual line instead of the entire string. |
| s | DotAll | Allows the `.` wildcard to match newline characters (`\n`, `\r`), enabling cross-line matching. |
| u | Unicode | Enables full UTF-16 surrogate pair handling and Unicode property escapes (e.g. `\p{Emoji}`). |
| v | Unicode Sets | Advanced ECMAScript 2024 flag adding set operations (difference/intersection) inside `[...]`. |
Preventing ReDoS (Catastrophic Backtracking)
Regular Expression Denial of Service (ReDoS) occurs when an NFA regex engine encounters nested quantifiers with overlapping match paths (e.g., (a+)+$). When evaluated against non-matching strings like "aaaaaaaaaaaaaaaaaaaaX", the engine explores exponential combinations ($O(2^n)$), freezing the CPU thread.
- Avoid Nested Quantifiers: Never write
(a+)*or([a-z]+)+. Refactor into atomic groups or non-overlapping character sets. - Bound Repeat Loops: Use exact bounds
{1,64}instead of unbounded+or*on user inputs. - Use Atomic Groups or Lookaheads: Where supported, use possessive quantifiers
++or atomic groups(?>...)to discard backtrack points. - Implement Execution Timeouts: When evaluating regexes on backend servers, enforce strict execution timeouts (e.g. 50ms) using worker threads.
Code Formatter & Converter Suite
Validate, format, minify, and convert JSON, SQL, YAML, XML, and code dialects directly on your local machine.
Frequently Asked Questions
Why does my regex match fail when tested with test() multiple times in JavaScript?
When a RegExp object has the global flag (`/g`), it retains internal state via the `lastIndex` property. Each `.test()` invocation starts scanning from `lastIndex` and advances it. Once a match reaches the end, `lastIndex` resets to 0. To avoid this bug, either omit the `/g` flag for boolean tests or reset `regex.lastIndex = 0` before testing.
What is the difference between non-capturing groups (?:...) and capturing groups (...) ?
A standard capturing group `(...)` stores the matched substring in memory so it can be referenced later (e.g., in match arrays or replacement strings like `$1`). A non-capturing group `(?:...)` groups tokens for quantifier application (e.g. `(?:abc)+`) without allocating memory for backreferences, resulting in faster execution.
Conclusion
`^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$`
Related Articles
How to Encode and Decode URLs: RFC 3986 Percent-Encoding, URLSearchParams & Polyglot Recipes
Complete guide to URL percent-encoding and decoding: RFC 3986 rules, query string construction, multi-byte UTF-8 handling, and code recipes in JS, Python, Go, and Shell.
Developer ToolsHow to Validate JSON: Syntax Diagnostics, JSON Schema (Draft 2020-12), and Runtime Validation
Learn how to validate JSON data: debugging syntax errors, defining JSON Schema (Draft 2020-12) contracts, and validating runtime payloads with Ajv, Zod, and Pydantic.
Developer ToolsMastering Regular Expressions: The Complete Developer Guide from Syntax to ReDoS Defense
Learn regular expressions from fundamentals to advanced engine mechanics: character classes, lookarounds, capturing groups, and catastrophic backtracking prevention.