Common Regular Expressions (RegEx) Library
Production-tested regular expressions for client-side forms, backend validation, and data extraction. Tested across JavaScript, Python, Go, PHP, and Java with sample matches and flag explanations.
Email Address Validation Regex
Standard RFC-compliant email address validation pattern for form inputs.
/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/iStrong Password Policy Regex
Enforces minimum 8 chars, at least 1 uppercase, 1 lowercase, 1 digit, and 1 special symbol.
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/HTTP/HTTPS URL Validation Regex
Validates web URLs including domain, optional port, and query parameter path.
/^https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&\/=]*)$/iIPv4 Address Validation Regex
Validates standard dotted decimal IPv4 network address within range 0.0.0.0 to 255.255.255.255.
/^(?:(?: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]?)$/UUID v4 Identifier Validation Regex
Validates 36-character canonical random UUID version 4 format.
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/iHex Color Code Regex (#RGB and #RRGGBB)
Matches 3-digit or 6-digit CSS hexadecimal color notation.
/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/iSEO Friendly URL Slug Regex
Enforces lowercase alphanumeric words separated only by single hyphens with no trailing dashes.
/^[a-z0-9]+(?:-[a-z0-9]+)*$/US Phone Number Format Regex
Validates 10-digit North American telephone numbers in formats like (123) 456-7890 or 123-456-7890.
/^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/ISO 8601 Date (YYYY-MM-DD) Regex
Matches standard ISO year-month-day calendar dates.
/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/JSON Web Token (JWT) Format Regex
Validates structure of Base64URL-encoded Header.Payload.Signature JWT strings.
/^[A-Za-z0-9-_=]+\.[A-Za-z0-9-_=]+\.?[A-Za-z0-9-_.+/=]*$/Semantic Versioning (SemVer) Regex
Validates official Semantic Versioning 2.0.0 strings including pre-release and build metadata.
/^v?(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-]+)*))?$/Base64 Encoded String Validation Regex
Validates standard RFC 4648 Base64 encoded strings with valid length multiples of 4 and padding.
/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/Hexadecimal Color Code (#HEX) Regex
Matches 3-digit shorthand, 6-digit RGB, or 8-digit RGBA hex color codes.
/^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/Credit Card Number (Visa / Master / Amex) Regex
Validates primary card major industry identifiers for Visa, MasterCard, American Express, and Discover.
/^(?: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})$/Hardware MAC Address Regex
Matches standard 48-bit physical MAC addresses formatted with colons or hyphens.
/^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/TCP / UDP Network Port (1-65535) Regex
Validates network port numbers strictly between 1 and 65,535.
/^([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])$/Git Commit Hash (SHA-1 / SHA-256) Regex
Matches short (7-char) and full (40-char SHA-1) Git commit hashes.
/^[0-9a-fA-F]{7,40}$/CUID & CUID2 Collision-Resistant ID Regex
Validates modern secure collision-resistant identifiers (CUID and CUID2) used in high-scale distributed systems.
/^[a-z0-9]{24,32}$/5-Field Unix Cron Expression Regex
Validates standard 5-to-7 field crontab expressions including wildcard, ranges, steps, and shortcuts.
/^(@(annually|yearly|monthly|weekly|daily|hourly|reboot))|(@every (\d+(ns|us|Β΅s|ms|s|m|h))+)|((((\d+,)+\d+|(\d+(\/|-)\d+)|\d+|\*) ?){5,7})$/NanoID (21-character) URL-Friendly ID Regex
Validates default 21-character URL-friendly, compact NanoID strings.
/^[A-Za-z0-9_-]{21}$/YouTube Video ID & URL Extractor Regex
Matches YouTube video URLs and captures the 11-character unique video identifier.
/^(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))([\w-]{11})(?:\S+)?$/iHTML Opening & Closing Tag Stripper Regex
Finds and strips opening, closing, and self-closing HTML/XML markup tags from rich text strings.
/<\/?[a-zA-Z][^>]*>/gMarkdown Image Syntax Regex
Extracts alt text, image URL, and optional title from standard Markdown image tags.
/!\[([^\]]*)\]\(([^\s)]+)(?:\s+"([^"]+)")?\)/gSemantic Versioning (SemVer 2.0.0) Regex
Official SemVer 2.0.0 regular expression supporting MAJOR.MINOR.PATCH, prerelease identifiers, 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-]+)*))?$/Docker Image Repository & Tag Name Regex
Validates complete Docker image references including optional registry domain, namespace, image name, and tag.
/^(?:(?=[^:\/]{1,253})(?!-)[a-zA-Z0-9-]{1,63}(?<!-)(?:\.(?!-)[a-zA-Z0-9-]{1,63}(?<!-))*(?::[0-9]{1,5})?\/)?(?:[a-z0-9]+(?:[._-][a-z0-9]+)*\/)*[a-z0-9]+(?:[._-][a-z0-9]+)*(?::[\w][\w.-]{0,127})?$/Un-hyphenated 32-Char Hexadecimal UUID Regex
Validates 32-character compact hexadecimal UUIDs/GUIDs stripped of standard hyphen delimiters.
/^[0-9a-fA-F]{32}$/SQL Injection Attack Detection Heuristic Regex
Detects common SQL injection attack signatures, comments, and dangerous stored procedures in user inputs.
/(?i)(?:\b(select|union|insert|update|delete|drop|alter|create|truncate|exec|execute)\b|--|/\*|\*/|xp_cmdshell|\bwaitfor\s+delay\b)/iIPv6 Address Validation Regex
Validates standard, compressed, and IPv4-mapped IPv6 network addresses compliant with RFC 4291.
/^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([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}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/iInternational E.164 Phone Number Regex
Strict ITU-T E.164 international phone number format beginning with plus sign followed by 2 to 15 digits.
/^\+[1-9]\d{1,14}$/United Kingdom Phone Number Regex
Comprehensive United Kingdom mobile, landline, and national dialing format validator.
/^(?:(?:\(?(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?(?:\(?0\)?[\s-]?)?)|(?:\(?0))(?:(?:\d{5}\)?[\s-]?\d{4,5})|(?:\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3}))|(?:\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4})|(?:\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}))(?:[\s-]?(?:x|ext\.?)[\s-]?\d{1,5})?$/iVisa Credit Card Number Regex
Matches 13-digit and standard 16-digit Visa debit and credit card numbers starting with 4.
/^4[0-9]{12}(?:[0-9]{3})?$/Mastercard Credit Card Number Regex
Validates 16-digit Mastercard card numbers across traditional 51-55 series and 2221-2720 2-series ranges.
/^(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}$/American Express (Amex) Card Regex
Validates 15-digit American Express credit cards starting with prefixes 34 or 37.
/^3[47][0-9]{13}$/ISO 8601 UTC / Timestamp Regex
Matches standard ISO 8601 and RFC 3339 formatted timestamps including millisecond precision and UTC/timezone offsets.
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/24-Hour Time (HH:mm) Regex
Validates 24-hour military and European clock time from 00:00 through 23:59.
/^(?:[01]\d|2[0-3]):[0-5]\d$/12-Hour Time with AM/PM Regex
Validates standard 12-hour time formats with case-insensitive AM or PM indicators.
/^(?:1[0-2]|0?[1-9]):[0-5]\d\s*(?:[AaPp][Mm])$/United States Postal ZIP Code Regex
Matches 5-digit US ZIP codes and standard 9-digit ZIP+4 formats (12345 or 12345-6789).
/^\d{5}(?:-\d{4})?$/United Kingdom Postcode Regex
Validates official UK postcodes across all outward and inward alphanumeric sectors.
/^[A-Z]{1,2}[0-9][A-Z0-9]? ?[0-9][A-Z]{2}$/iCanada Postal Code Regex
Validates Canadian postal codes adhering to official Canada Post character exclusions (excludes D, F, I, O, Q, U).
/^[A-CEGHJ-NPR-TVXY]\d[A-CEGHJ-NPR-TV-Z] ?\d[A-CEGHJ-NPR-TV-Z]\d$/iBitcoin (BTC) Address Regex
Validates Bitcoin Legacy (P2PKH starting with 1), SegWit P2SH (starting with 3), and Native SegWit (Bech32 starting with bc1) wallet addresses.
/^(?:1[a-km-zA-HJ-NP-Z1-9]{25,34}|3[a-km-zA-HJ-NP-Z1-9]{25,34}|bc1[a-zA-HJ-NP-Z0-9]{25,39})$/Ethereum (ETH) Address Regex
Validates Ethereum and EVM-compatible (Polygon, Arbitrum, BSC) public account and contract hexadecimal addresses.
/^0x[a-fA-F0-9]{40}$/Twitter / X Username Handle Regex
Validates Twitter and X user handles, allowing an optional leading @ symbol and up to 15 alphanumeric or underscore characters.
/^@?[a-zA-Z0-9_]{1,15}$/GitHub Username Regex
Strictly complies with GitHub username rules: max 39 chars, alphanumeric, single internal hyphens, no consecutive hyphens, cannot start or end with a hyphen.
/^[a-zA-Z0-9](?:[a-zA-Z0-9]|-(?=[a-zA-Z0-9])){0,38}$/Fully Qualified Domain Name (FQDN) Regex
RFC 1035 compliant domain name and hostname validator preventing leading/trailing hyphens in labels.
/^(?!-)[A-Za-z0-9-]{1,63}(?<!-)\.(?:[A-Za-z]{2,}|(?:[A-Za-z0-9-]{1,63}(?<!-)\.)+[A-Za-z]{2,})$/Subdomain Extraction Regex
Captures and isolates the third-level subdomain prefix from a full hostname or URL.
/^(?:https?:\/\/)?([a-zA-Z0-9-]+)\.[a-zA-Z0-9-]+\.[a-zA-Z]{2,}/iFile Extension Extraction Regex
Isolates the trailing file extension from local paths, URLs, or document names while safely ignoring query strings and hashes.
/\.([a-zA-Z0-9]+)(?:\?|#|$)/iCSS RGB / RGBA Color Function Regex
Matches valid CSS rgb(255, 255, 255) and rgba(0, 0, 0, 0.5) declarations restricting channels to 0-255.
/^rgba?\(\s*(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\s*,\s*(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\s*,\s*(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\s*(?:,\s*(?:0|1|0?\.\d+)\s*)?\)$/iCSS HSL / HSLA Color Function Regex
Validates CSS hsl() and hsla() syntax, clamping hue to 0-360 and saturation/lightness to 0%-100%.
/^hsla?\(\s*(?:360|3[0-5]\d|[12]?\d{1,2})\s*,\s*(?:100|[1-9]?\d)%\s*,\s*(?:100|[1-9]?\d)%\s*(?:,\s*(?:0|1|0?\.\d+)\s*)?\)$/iHTML Attribute Value Extractor Regex
Extracts HTML attribute key-value pairs (e.g., href="...", class="...", data-id="...") from markup tags.
/\b([a-zA-Z-]+)=["\']([^"\']+)["\']/gMarkdown Link Extractor Regex
Parses and extracts markdown hyperlink text labels and target destination URLs [Title](https://...).
/\[([^\]]+)\]\((https?:\/\/[^\s\)]+)\)/gCamelCase Word Boundary Regex
Identifies transitional word boundaries in camelCase and PascalCase identifiers to convert strings into snake_case or kebab-case.
/([a-z0-9])([A-Z])/gDuplicate Consecutive Word Regex
Finds accidental repeated words in editorial prose and code comments (e.g., "the the", "in in").
/\b([a-zA-Z]+)\s+\1\b/iLeading & Trailing Whitespace Regex
Targets superfluous leading and trailing spaces or tab characters across string lines for clean sanitization.
/^\s+|\s+$/gC-Style Code Comment Regex
Matches single-line (//) and multi-line (/* ... */) code comments across JavaScript, C, Java, Go, and PHP.
/\/\*[\s\S]*?\*\/|\/\/.*/gJSON Key-Value Pair Extractor Regex
Extracts property keys and primitive values (strings, numbers, booleans, null) from JSON data strings.
/"([^"]+)"\s*:\s*("[^"]*"|\d+(?:\.\d+)?|true|false|null)/gRegular Expression Syntax Quick Reference
Core metacharacters, quantifiers, and assertion syntax
| Token | Description | Example |
|---|---|---|
| ^ / $ | Start of line / End of line anchors | ^https? |
| \d / \D | Any digit [0-9] / Any non-digit | \d4-\d2-\d2 |
| \w / \W | Word character [a-zA-Z0-9_] / Non-word | ^\w+$ |
| \s / \S | Whitespace character (space, tab, newline) / Non-whitespace | \s+ |
| *, +, ? | 0 or more (*), 1 or more (+), 0 or 1 optional (?) | colou?r |
| (?=...) | Positive lookahead assertion (must be followed by ...) | (?=.*[A-Z]) |
Frequently Asked Questions About Regular Expressions
Frequently Asked Questions
Everything you need to know regarding specifications, syntax, and security best practices.
For general web form validation, `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$` provides the optimal balance between strictness and compatibility without rejecting valid modern domains.