Regex Cheatsheet
A quick reference for regular-expression syntax. Every token with its meaning and an example.
Anchors
| Token | Meaning | Example |
|---|---|---|
^ | Start of string (or line with m) | ^Hello |
$ | End of string (or line with m) | world$ |
\b | Word boundary | \bcat\b |
\B | Not a word boundary | \Bcat |
Character classes
| Token | Meaning | Example |
|---|---|---|
. | Any character except newline | a.c |
\d | Any digit, 0–9 | \d\d |
\D | Any non-digit | \D+ |
\w | Any word character (a–z, A–Z, 0–9, _) | \w+ |
\W | Any non-word character | \W |
\s | Any whitespace character | a\sb |
\S | Any non-whitespace character | \S+ |
[abc] | Any one character in the set | [aeiou] |
[^abc] | Any character not in the set | [^0-9] |
[a-z] | Any character in the range a to z | [A-Z] |
Quantifiers
| Token | Meaning | Example |
|---|---|---|
* | Zero or more of the preceding | ab* |
+ | One or more of the preceding | ab+ |
? | Zero or one (optional) | ab? |
{n} | Exactly n of the preceding | \d{4} |
{n,} | n or more of the preceding | \d{2,} |
{n,m} | Between n and m of the preceding | \d{2,4} |
*? | Lazy — match as few as possible | <.*?> |
Groups & lookaround
| Token | Meaning | Example |
|---|---|---|
(abc) | Capturing group | (\d+) |
(?:abc) | Non-capturing group | (?:ab)+ |
(?<name>) | Named capturing group | (?<year>\d{4}) |
a|b | Alternation — a or b | cat|dog |
(?=abc) | Positive lookahead | \d(?=px) |
(?!abc) | Negative lookahead | \d(?!px) |
(?<=abc) | Positive lookbehind | (?<=\$)\d+ |
(?<!abc) | Negative lookbehind | (?<!\$)\d+ |
\1 | Backreference to group 1 | (\w)\1 |
Special & escapes
| Token | Meaning | Example |
|---|---|---|
\. | A literal dot (escape special chars) | 3\.14 |
\uXXXX | Unicode character by code point | é |
Flags
| Token | Meaning | Example |
|---|---|---|
g | Global — find all matches | /\d+/g |
i | Case-insensitive matching | /abc/i |
m | Multiline — ^ and $ match line breaks | /^x/m |
s | Dotall — . also matches newlines | /a.b/s |
u | Unicode mode | /\u{1F600}/u |
y | Sticky — match from lastIndex only | /\d/y |
Try it yourself
Head to the tester to experiment with any of these tokens live.