Email Address
Practical pattern that catches the email addresses you actually encounter without trying to implement full RFC 5322.
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
A curated library of practical regex patterns for everyday validation and extraction tasks — email, URL, IP addresses, phone numbers, dates, UUIDs, hex colors, credit cards, semver, and more. Each pattern is annotated with the cases it matches and the trade-offs it makes, so you can pick the right one without rereading the RFC.
Practical pattern that catches the email addresses you actually encounter without trying to implement full RFC 5322.
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
Loose pattern that finds candidate URLs in text. Pair with a real URL parser for structural validation.
https?:\/\/[a-zA-Z0-9.-]+(?::\d+)?(?:\/[^\s]*)?
Matches fully-qualified domain names with one or more labels and a TLD of at least two letters.
(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}
Quick dotted-quad match. Allows octets up to 999 — fast for extraction, not for validation.
\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
Validates each octet falls in the legal 0–255 range.
\b(?:25[0-5]|2[0-4]\d|[01]?\d\d?)(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)){3}\b
Matches full and compressed IPv6 forms including :: and IPv4-mapped variants.
(?:(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|(?:[0-9a-fA-F]{1,4}:){1,7}:|::(?:[0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4}|(?:[0-9a-fA-F]{1,4}:){1,6}(?::[0-9a-fA-F]{1,4}){1,1}|(?:[0-9a-fA-F]{1,4}:){1,5}(?::[0-9a-fA-F]{1,4}){1,2}|(?:[0-9a-fA-F]{1,4}:){1,4}(?::[0-9a-fA-F]{1,4}){1,3}|(?:[0-9a-fA-F]{1,4}:){1,3}(?::[0-9a-fA-F]{1,4}){1,4}|(?:[0-9a-fA-F]{1,4}:){1,2}(?::[0-9a-fA-F]{1,4}){1,5})
Matches MAC addresses with either colons or hyphens as separators.
(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}
Tolerant US phone matcher accepting country code, parentheses, dashes, dots, and spaces.
(?:\+?1[-.\s]?)?\(?(\d{3})\)?[-.\s]?(\d{3})[-.\s]?(\d{4})
Matches numbers in E.164 international format with optional plus and 7–15 digits.
\+?[1-9]\d{1,14}
5-digit ZIP code with optional ZIP+4 extension.
\b\d{5}(?:-\d{4})?\b
Royal Mail format. Letters case-insensitive; the central space is optional.
\b[A-Z]{1,2}\d[A-Z\d]?\s*\d[A-Z]{2}\b
Canadian format A1A 1A1, with optional space.
\b[ABCEGHJ-NPRSTVXY]\d[A-Z]\s?\d[A-Z]\d\b
Matches any RFC 4122 UUID (any version) in canonical hyphenated form.
\b[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-7][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}\b
Standard username rules: 3–20 characters, letters, digits, and underscore, must start with a letter.
^[a-zA-Z][a-zA-Z0-9_]{2,19}$
Lowercase letters, digits, and hyphens, no leading/trailing hyphen.
^[a-z0-9]+(?:-[a-z0-9]+)*$
Demands at least 8 characters with at least one lowercase, one uppercase, one digit, and one symbol. Uses lookahead.
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*]).{8,}$
Detects Visa, MasterCard, American Express, Discover, JCB, and Diners Club by prefix and length.
(?:4\d{12}(?:\d{3})?|(?:5[1-5]\d{2}|2[2-7]\d{2})\d{12}|3[47]\d{13}|6(?:011|5\d{2})\d{12}|(?:2131|1800|35\d{3})\d{11}|3(?:0[0-5]|[68]\d)\d{11})
CSS-style hex colors with optional alpha channel.
#(?:[0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})\b
CSS rgb() and rgba() function calls with optional alpha.
rgba?\(\s*\d{1,3}\s*,\s*\d{1,3}\s*,\s*\d{1,3}\s*(?:,\s*(?:0|1|0?\.\d+))?\s*\)
ISO 8601 calendar date with strict month and day ranges.
\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])
Full datetime with optional milliseconds and timezone offset (or Z for UTC).
\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)
24-hour HH:MM with optional seconds.
(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?
Whole number with optional sign. No leading zeros.
-?(?:0|[1-9]\d*)
Signed integer or decimal with optional fractional part.
-?\d+(?:\.\d+)?
Numbers in scientific (E-notation) form.
-?\d+(?:\.\d+)?[eE][+-]?\d+
Hex literal with 0x prefix. Convert with the base converter.
0[xX][0-9a-fA-F]+
SemVer 2.0.0 — major.minor.patch with optional pre-release and build metadata.
(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 an opening, closing, or self-closing HTML tag with attributes.
<\/?[a-zA-Z][a-zA-Z0-9-]*(?:\s+[a-zA-Z][a-zA-Z0-9-]*(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s>]+))?)*\s*\/?>
Inline Markdown link [text](url) with optional title.
\[([^\]]+)\]\(([^\s)]+?)(?:\s+"([^"]+)")?\)
Match whitespace at the start or end of a string. Replace with empty for trim.
^\s+|\s+$
Captures the trailing extension of a filename (after the last dot).
\.([a-zA-Z0-9]+)$
Matches a hashtag with letters, digits, and underscores after the #.
(?:^|\s)#([a-zA-Z][a-zA-Z0-9_]*)
Matches @-handle: 1–15 alphanumerics or underscores.
(?:^|\s)@([A-Za-z0-9_]{1,15})\b
Three Base64URL segments joined by dots.
[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+
email, ipv4, semver, color, etc.Most "definitive" regex patterns floating around the internet are either too loose (matching obvious nonsense) or too strict (rejecting real input). This library leans pragmatic: each pattern catches the cases you actually encounter in form inputs, log files, and user-generated content, while the Note line is honest about what it skips. The full RFC for an email address runs to several pages and accepts strings most users would consider clearly invalid; the email pattern here is the same one used by most real-world validation libraries because it works.
Two general principles for using regex on real data: first, regex is for finding, not validating. Use it to extract candidate values, then validate each one with a proper parser (new URL(), parseFloat(), the Luhn algorithm). Second, readable patterns beat clever patterns. A 200-character regex that handles every edge case is harder to maintain than a 50-character regex plus a few lines of follow-up validation code. Pick the version that future-you will be able to read.