Regex Cheat Sheet
A searchable regular expression cheat sheet: character classes, anchors, quantifiers, groups, lookarounds, flags, and tested patterns for emails, IPs, UUIDs, dates, semver and slugs.
Free · No sign-up · Your inputs stay in your browser
Reference
Regular expression cheat sheet
| Pattern | Matches | Notes |
|---|---|---|
| . | Any character except a newline | With the s flag (dotAll), newlines too. |
| \d \w \s | Digit, word character, whitespace | \D \W \S are the negations. With the u flag, \d is still only 0-9 in JavaScript. |
| [abc] [^abc] [a-z] | One of, none of, a range | Inside a class most metacharacters are literal; escape ] \ ^ -. |
| ^ $ | Start and end of the string | Of each line with the m flag. |
| \b \B | Word boundary, not a boundary | \bcat\b matches cat but not concatenate. |
| * + ? | 0 or more, 1 or more, 0 or 1 | Greedy: they take as much as possible. |
| {3} {2,} {2,5} | Exactly, at least, between | |
| *? +? ?? | Lazy versions | Take as little as possible: <.+?> stops at the first >. |
| (abc) | Capturing group | $1 or \1 in the replacement. |
| (?<name>abc) | Named group | $<name> in JavaScript replacements; (?P<name>) in Python and older PCRE. |
| (?:abc) | Non-capturing group | Groups without capturing; faster and keeps numbering simple. |
| a|b | Alternation | Lowest precedence: ^a|b$ means (^a)|(b$). |
| \1 \k<name> | Backreference | (['"]).*?\1 matches a quoted string with matching quotes. |
| (?=x) (?!x) | Lookahead, negative lookahead | Match a position without consuming: \d+(?=px). |
| (?<=x) (?<!x) | Lookbehind, negative lookbehind | Supported in all current browsers, PCRE and Python (fixed width in Python). |
| \p{L} \p{N} | Unicode letter, number | Needs the u flag in JavaScript; works in PCRE with /u. |
| g i m s u y | JavaScript flags | global, case-insensitive, multiline, dotAll, unicode, sticky. v is the newer unicode-sets flag. |
| ^[\w.+-]+@[\w-]+(\.[\w-]+)+$ | Email (practical) | Good enough for a form; RFC 5322 allows more. Confirm by sending a link. |
| ^https?:\/\/[^\s/$.?#].[^\s]*$ | URL (http/https) | Prefer new URL() and catch the error in code. |
| ^(25[0-5]|2[0-4]\d|1?\d?\d)(\.(25[0-5]|2[0-4]\d|1?\d?\d)){3}$ | IPv4 address | Rejects 256 and above in each octet. |
| ^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$ | UUID | Use the i flag. |
| ^#?([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$ | Hex colour | 3, 6 or 8 digits (with alpha). |
| ^\d{4}-\d{2}-\d{2}$ | ISO date (shape only) | Accepts 2026-02-31; validate with a date library. |
| ^([01]\d|2[0-3]):[0-5]\d$ | 24-hour time | |
| ^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-[\w.-]+)?(\+[\w.-]+)?$ | Semantic version | Simplified from the official semver.org regex. |
| ^[a-z0-9]+(?:-[a-z0-9]+)*$ | URL slug | Lowercase words joined by single hyphens. |
| ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{12,}$ | Password with mixed case and a digit | Length matters more than classes; NIST 800-63B advises against composition rules. |
| ^\s+|\s+$ | Leading or trailing whitespace | Replace with '' and the g flag to trim. |
| (\d)(?=(\d{3})+(?!\d)) | Thousands separator positions | Replace with $1, to get 1,234,567. |
JavaScript syntax, which PCRE (PHP's preg_*), Python's re and most editors share for everything above except where noted. Test any pattern against real input with the regex tester before relying on it, and anchor it with ^ and $ when validating, or it matches a substring.
About this tool
Method, assumptions, practical next steps, and limits.
One syntax, small differences
Everything here is JavaScript syntax, which PCRE (PHP's preg functions), Python's re module and most editors share for the common cases. The differences show at the edges: named groups are (?P<name>) in older Python and PCRE, Python's lookbehind must be fixed width, and only some flavours support possessive quantifiers. MDN's regex cheat sheet covers JavaScript in detail.
Validation patterns
The email and URL patterns are practical, not complete: confirm an email by sending a link and parse URLs with a URL parser. Always anchor validation patterns with ^ and $, or they accept any string that contains a match.