← All tools
Regex Cheat Sheet
Quick reference for regular expression syntax
Anchors
| Syntax | Description | Example | |
|---|---|---|---|
^ | Start of string (or line with m flag) | ^Hello | |
$ | End of string (or line with m flag) | world$ | |
\b | Word boundary | \bcat\b | |
\B | Non-word boundary | \Bcat\B |
Character Classes
| Syntax | Description | Example | |
|---|---|---|---|
\d | Any digit [0-9] | \d+ | |
\D | Non-digit | \D+ | |
\w | Word character [a-zA-Z0-9_] | \w+ | |
\W | Non-word character | \W+ | |
\s | Whitespace (space, tab, newline) | \s+ | |
\S | Non-whitespace | \S+ | |
. | Any character except newline | a.b |
Quantifiers
| Syntax | Description | Example | |
|---|---|---|---|
* | 0 or more (greedy) | a* | |
+ | 1 or more (greedy) | a+ | |
? | 0 or 1 (optional) | colou?r | |
{n} | Exactly n times | \d{4} | |
{n,} | n or more times | \d{2,} | |
{n,m} | Between n and m times | \d{2,4} | |
*? | 0 or more (lazy/non-greedy) | a.*?b | |
+? | 1 or more (lazy) | a.+?b |
Groups
| Syntax | Description | Example | |
|---|---|---|---|
(abc) | Capture group | (foo)bar\1 | |
(?:abc) | Non-capturing group | (?:foo|bar)+ | |
(?<name>abc) | Named capture group | (?<year>\d{4}) | |
(a|b) | Alternation (OR) | (cat|dog) |
Lookaround
| Syntax | Description | Example | |
|---|---|---|---|
(?=...) | Positive lookahead | \d+(?= dollars) | |
(?!...) | Negative lookahead | \d+(?! dollars) | |
(?<=...) | Positive lookbehind | (?<=\$)\d+ | |
(?<!...) | Negative lookbehind | (?<!\$)\d+ |
Character Sets
| Syntax | Description | Example | |
|---|---|---|---|
[abc] | Any of a, b, or c | [aeiou] | |
[^abc] | Not a, b, or c | [^\d] | |
[a-z] | Range a to z | [a-zA-Z] | |
[A-Z0-9] | Uppercase letters or digits | [A-Z0-9_] |
Flags
| Syntax | Description | Example | |
|---|---|---|---|
g | Global — find all matches | /pattern/g | |
i | Case-insensitive | /hello/i | |
m | Multiline — ^ and $ match line boundaries | /^line/m | |
s | dotAll — . matches newline too | /a.b/s | |
u | Unicode mode | /\u{1F600}/u | |
y | Sticky — match at lastIndex only | /\d/y |
Common Patterns
| Syntax | Description | Example | |
|---|---|---|---|
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,} | Email address | user@example.com | |
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&/=]*) | URL | https://example.com/path?q=1 | |
\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b | IPv4 address | 192.168.1.1 | |
^#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$ | Hex color code | #FF5733 | |
^\d{4}-\d{2}-\d{2}$ | Date YYYY-MM-DD | 2024-01-15 | |
^(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\d{3}\)|\d{3})\s*(?:[.-]\s*)?\d{3}\s*(?:[.-]\s*)?\d{4}$ | US Phone number | +1 (555) 123-4567 | |
^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13})$ | Credit card (Visa/MC/Amex) | 4111111111111111 |