Regex Cheat Sheet
Regular expression syntax and flags, with examples.
About Regex cheat sheet
Regular expressions describe text patterns with a compact syntax: classes say which characters can appear, quantifiers say how many times, anchors say where, and groups capture parts of a match or combine alternatives. The same core syntax works in JavaScript, Python, Java, Go, PCRE (PHP, many editors) and grep with -E, with differences in advanced features.
This sheet follows JavaScript (ECMAScript), the flavor the regex tester uses. Notable differences elsewhere: Python spells named groups (?P<name>...), Go's RE2 has no lookaround or backreferences by design, and POSIX tools use classes like [[:digit:]] instead of \d. Try any pattern on your own text in the regex tester before you rely on it.
54 entries
Characters and classes
| Syntax | Meaning | Example |
|---|---|---|
| . | Any character except a line break (any character with the s flag) | a.c matches abc, a-c |
| \d | A digit, 0-9 | \d\d matches 42 |
| \D | Not a digit | |
| \w | A word character: A-Z, a-z, 0-9 or _ | \w+ matches user_1 |
| \W | Not a word character | |
| \s | Whitespace: space, tab, line breaks and other Unicode spaces | |
| \S | Not whitespace | |
| [abc] | One of a, b or c | gr[ae]y matches gray and grey |
| [^abc] | Any character except a, b or c | |
| [a-z] | A character in the range a to z | [A-Fa-f0-9] matches a hex digit |
| \t \n \r | Tab, line feed, carriage return | |
| \uFFFF | A character by its UTF-16 code unit | \u00e9 matches é |
| \u{1F600} | A code point (needs the u flag) | |
| \p{L} | A Unicode property: any letter (needs the u flag) | \p{Lu} uppercase, \p{N} number |
| \. | A literal dot: escape . * + ? ^ $ ( ) [ ] { } | \ / with a backslash | \.com matches .com |
Anchors and boundaries
| Syntax | Meaning | Example |
|---|---|---|
| ^ | Start of the string (start of each line with the m flag) | ^Hello |
| $ | End of the string (end of each line with the m flag) | world$ |
| \b | A word boundary | \bcat\b matches cat but not concat |
| \B | Not a word boundary |
Quantifiers
| Syntax | Meaning | Example |
|---|---|---|
| * | 0 or more | ab*c matches ac, abc, abbc |
| + | 1 or more | ab+c matches abc, abbc |
| ? | 0 or 1 (optional) | colou?r matches color and colour |
| {3} | Exactly 3 | \d{3} matches 123 |
| {2,} | 2 or more | |
| {2,5} | Between 2 and 5 | |
| *? +? ?? | Lazy: as few as possible | <.+?> matches one tag at a time |
Groups, alternation and lookaround
| Syntax | Meaning | Example |
|---|---|---|
| (abc) | Capturing group | (\d+)-(\d+) captures both numbers |
| (?<name>abc) | Named capturing group | (?<year>\d{4}) |
| (?:abc) | Group without capturing | (?:https?://)? |
| a|b | a or b | cat|dog |
| \1 | Backreference to group 1 | (\w)\1 matches a doubled letter |
| \k<name> | Backreference to a named group | |
| (?=abc) | Lookahead: followed by abc | \d+(?=px) matches 12 in 12px |
| (?!abc) | Negative lookahead: not followed by abc | |
| (?<=abc) | Lookbehind: preceded by abc | (?<=\$)\d+ matches 5 in $5 |
| (?<!abc) | Negative lookbehind: not preceded by abc |
JavaScript flags
| Syntax | Meaning | Example |
|---|---|---|
| g | Global: find all matches, not just the first | /a/g |
| i | Ignore case | /hello/i matches HELLO |
| m | Multiline: ^ and $ match at line breaks | |
| s | Dot-all: . also matches line breaks | |
| u | Unicode: code points and \p{...} escapes | |
| v | Unicode sets: u plus set operations in classes (ES2024) | |
| y | Sticky: match only at lastIndex | |
| d | Indices: report start and end of each group |
Handy patterns
Starting points, not complete validators: real-world formats have edge cases, so test with your own data.
| Syntax | Meaning | Example |
|---|---|---|
| ^\d+$ | Whole string is digits | 12345 |
| ^-?\d+(\.\d+)?$ | Integer or decimal number | -3.14 |
| ^[^\s@]+@[^\s@]+\.[^\s@]+$ | Rough email shape (something@something.tld) | ada@example.com |
| ^https?://\S+$ | A URL starting with http:// or https:// | |
| ^\d{4}-\d{2}-\d{2}$ | Date shaped like YYYY-MM-DD (doesn't check the date exists) | 2026-10-02 |
| ^#?([0-9a-f]{3}|[0-9a-f]{6})$ | Hex color, with the i flag | #33aabb |
| ^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$ | UUID, with the i flag | |
| ^\s+|\s+$ | Leading or trailing whitespace (use with g to trim) | |
| \s{2,} | Runs of two or more whitespace characters | |
| ^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}$ | At least 8 characters with a digit, a lowercase and an uppercase letter |
Good to know
Avoid catastrophic backtracking
Nested quantifiers such as (a+)+ or (.*)*, or alternatives that can match the same text, can take exponential time on input that almost matches. On a server that's a denial-of-service risk (ReDoS). Make each part of the pattern match as specifically as possible.
More references
- HTTP status codesEvery HTTP status code and what it means
- MIME typesCommon MIME types by file extension
- ASCII tableAll 128 ASCII codes in dec, hex, octal and binary
- HTML entitiesNamed HTML entities with their codes
- Cron examples30 common cron schedules explained
- Chmod examplesCommon Unix permissions like 644 and 755
- Unix timestamp referenceNotable epochs, the 2038 problem and code
- Git cheat sheetEveryday Git commands in one place
- Markdown cheat sheetMarkdown and GitHub Flavored syntax