Regex cheatsheet
Regular expressions are compact enough that the syntax is easier to look up than to memorise. This page is the whole of it on one screen — classes, quantifiers, anchors, groups, lookaround, escaping and the flags — with a working example for every row.
The flavour is JavaScript, which is what runs in your browser and what the regex tester on this site uses. Most of it is identical in Python, Java, Go and PCRE; where flavours diverge, they diverge on the edges — lookbehind support, named-group syntax, and whether \d means "ASCII digit" or "any Unicode digit".
Two things account for most of the surprises. Quantifiers are greedy: they take everything they can and hand back only when the rest of the pattern fails, which is why <.+> swallows two tags and <.+?> does not. And alternation binds loosest of all, so ^a|b$ means "starts with a, or ends with b" — not what most people intend when they write it.
Character classes
The shorthands are ASCII-only unless you add the u flag and use Unicode property escapes.
| Syntax | Meaning | Example |
|---|---|---|
| . | Any character except a line breakAdd the s flag to make it match newlines too. | /a.c/"abc" → "abc" |
| \d | A digit, 0–9 | /\d+/"order 42" → "42" |
| \D | Anything that is not a digit | /\D+/"42abc" → "abc" |
| \w | Letter, digit or underscoreASCII only — "é" is not \w. | /\w+/"user_1!" → "user_1" |
| \W | Anything \w does not match | /\W+/"a++b" → "++" |
| \s | Any whitespace, including tabs and newlines | /\s+/"a \t b" → " \t " |
| \S | Any non-whitespace character | /\S+/" hi" → "hi" |
| [abc] | Any one of the listed characters | /[aeiou]/"rhythm and" → "a" |
| [^abc] | Any character except the listed ones | /[^aeiou ]+/"aeiou xyz" → "xyz" |
| [a-z] | A range of characters | /[a-f]+/"zzabcz" → "abc" |
| \p{L} | Any Unicode letter (needs the u flag) | /\p{L}+/"42 café" → "café" |
Quantifiers
All of these are greedy by default: they take as much as they can and give back only if the rest of the pattern fails.
| Syntax | Meaning | Example |
|---|---|---|
| * | Zero or more | /ab*/"a" → "a" |
| + | One or more | /ab+/"abbb" → "abbb" |
| ? | Zero or one — makes it optional | /colou?r/"color" → "color" |
| {3} | Exactly three | /\d{3}/"id 12345" → "123" |
| {2,} | Two or more | /a{2,}/"aaa" → "aaa" |
| {2,4} | Between two and four | /a{2,4}/"aaaaa" → "aaaa" |
| *? | Lazy — takes as little as possibleThe greedy <.+> would swallow both tags. | /<.+?>/"<a><b>" → "<a>" |
Anchors and boundaries
These match a position, not a character, so they consume nothing.
| Syntax | Meaning | Example |
|---|---|---|
| ^ | Start of the string, or of a line with the m flag | /^a/"abc" → "a" |
| $ | End of the string, or of a line with the m flag | /c$/"abc" → "c" |
| \b | A word boundaryThis is what stops "cat" matching inside "concatenate". | /\bcat\b/"the cat sat" → "cat" |
| \B | Not a word boundary | /\Bcat/"concatenate" → "cat" |
Groups and alternation
| Syntax | Meaning | Example |
|---|---|---|
| (abc) | Capturing group — remembered for replacement | /(\d+)-(\d+)/"10-20" → "10-20" |
| (?:abc) | Grouping without capturingUse this when you only need the grouping; it keeps capture numbers meaningful. | /(?:ab)+/"abab" → "abab" |
| (?<name>…) | Named capturing group | /(?<year>\d{4})/"in 2026" → "2026" |
| a|b | Either side — alternationAlternation has the lowest precedence: ^a|b$ is "^a" or "b$", not "^(a|b)$". | /cat|dog/"one dog" → "dog" |
| \1 | Backreference to the first group | /(\w)\1/"letter" → "tt" |
Lookaround
Assertions about what surrounds a position. They match nothing themselves, which is what makes them safe to combine.
| Syntax | Meaning | Example |
|---|---|---|
| (?=…) | Lookahead — followed by this | /\d+(?= kg)/"80 kg" → "80" |
| (?!…) | Negative lookahead — not followed by this | /\d+(?! kg)/"80 lb" → "80" |
| (?<=…) | Lookbehind — preceded by this | /(?<=\$)\d+/"costs $40" → "40" |
| (?<!…) | Negative lookbehind — not preceded by this | /(?<!\$)\b\d+/"about 40" → "40" |
Escaping
These twelve characters are the ones that need a backslash to be taken literally.
| Syntax | Meaning | Example |
|---|---|---|
| \. | A literal dot | /\d\.\d/"v2.5" → "2.5" |
| \\ | A literal backslash | /\\/"C:\\dir" → "\\" |
| \n | A line feed | /a\nb/"a\nb" → "a\nb" |
| \t | A tab | /a\tb/"a\tb" → "a\tb" |
| \uFFFF | A character by its code point | /\u00e9/"café" → "é" |
Flags
| Flag | Name | Meaning |
|---|---|---|
| g | Global | Find every match, not just the first. |
| i | Ignore case | Match regardless of upper or lower case. |
| m | Multiline | Makes ^ and $ match at every line break instead of only at the ends of the string. |
| s | Dot all | Lets . match line breaks too. |
| u | Unicode | Treats the pattern as code points and enables \p{…} property escapes. |
| y | Sticky | Matches only at exactly lastIndex, never searching forward. |