Regex Cheat Sheet: Regular Expression Syntax Reference
Regular expressions (regex) are patterns for matching text — used for search-and-replace, validation, and extraction. The syntax below is the common flavor shared by JavaScript, Python, PCRE, and most modern engines. Search any token to jump to it.
Updated 2026-07-06
Character classes
| Token | Matches | Example |
|---|---|---|
| . | Any character except a newline | a.c → abc, a7c |
| \d | Any digit (0–9) | \d\d → 42 |
| \D | Any non-digit | \D → a, % |
| \w | Word character (letter, digit, underscore) | \w+ → hello_1 |
| \W | Non-word character | \W → space, ! |
| \s | Whitespace (space, tab, newline) | a\sb → a b |
| \S | Non-whitespace | \S+ → word |
| [abc] | Any one of a, b, or c | [aeiou] → a vowel |
| [^abc] | Any character except a, b, c | [^0-9] → non-digit |
| [a-z] | Any character in the range | [a-f] → a…f |
Anchors & boundaries
| Token | Matches | Example |
|---|---|---|
| ^ | Start of the string (or line, in multiline mode) | ^Hi → line starting Hi |
| $ | End of the string (or line) | end$ → line ending end |
| \b | Word boundary | \bcat\b → the word cat |
| \B | Not a word boundary | \Bcat → scatter |
Quantifiers
| Token | Matches | Example |
|---|---|---|
| * | 0 or more of the preceding | a* → '', a, aaa |
| + | 1 or more | a+ → a, aaa (not '') |
| ? | 0 or 1 (optional) | colou?r → color, colour |
| {n} | Exactly n times | \d{4} → 2026 |
| {n,} | n or more times | \d{2,} → 42, 4200 |
| {n,m} | Between n and m times | \d{2,4} → 42 to 4200 |
| *? | Lazy: as few as possible | <.+?> → shortest match |
Groups, alternation & lookarounds
| Token | Matches | Example |
|---|---|---|
| (abc) | Capturing group | (ab)+ → abab |
| (?:abc) | Non-capturing group | (?:ab)+ → group, no capture |
| (?<name>…) | Named capturing group | (?<year>\d{4}) |
| a|b | Alternation — a or b | cat|dog → cat, dog |
| \1 | Backreference to group 1 | (\w)\1 → ll, oo |
| (?=abc) | Lookahead — followed by abc | \d(?=px) → 5 in 5px |
| (?!abc) | Negative lookahead — not followed by | \d(?!px) |
| (?<=abc) | Lookbehind — preceded by abc | (?<=\$)\d+ → 9 in $9 |
| (?<!abc) | Negative lookbehind | (?<!\$)\d+ |
Flags
| Token | Matches | Example |
|---|---|---|
| g | Global — find all matches, not just the first | /a/g |
| i | Case-insensitive | /abc/i → ABC |
| m | Multiline — ^ and $ match line breaks | /^x/m |
| s | Dotall — . also matches newlines | /a.b/s |
| u | Unicode mode | /\u{1F600}/u |
A note on escaping
To match a character that has special meaning — such as . * + ? ( ) [ ] { } ^ $ | \\ — put a backslash in front of it. For example, \\. matches a literal dot and \\$ matches a literal dollar sign. When a pattern isn't matching what you expect, an unescaped special character is the usual cause.
Related tools