Regex Cheat Sheet
Everything you'll reach for when writing or reading a regex. JavaScript syntax, other flavours are similar but check your engine's docs for lookbehind, named groups, and Unicode rules.
Character classes
. | Any character except newline |
\d | Digit (0–9) |
\D | Non-digit |
\w | Word character (a–z, A–Z, 0–9, _) |
\W | Non-word character |
\s | Whitespace (space, tab, newline) |
\S | Non-whitespace |
[abc] | Any of a, b, or c |
[^abc] | Anything except a, b, or c |
[a-z] | Range (lowercase letters) |
Anchors
^ | Start of string (or line, with /m) |
$ | End of string (or line, with /m) |
\b | Word boundary |
\B | Non-word boundary |
Quantifiers
* | 0 or more |
+ | 1 or more |
? | 0 or 1 (optional) |
{n} | Exactly n |
{n,} | n or more |
{n,m} | Between n and m |
*? +? ?? | Lazy (non-greedy) versions, match as little as possible |
Groups & alternation
(abc) | Capturing group |
(?:abc) | Non-capturing group |
(?<name>abc) | Named capturing group |
a|b | Either a or b |
\1, \2… | Backreference to capturing group |
Lookarounds
(?=abc) | Lookahead, followed by abc |
(?!abc) | Negative lookahead. NOT followed by abc |
(?<=abc) | Lookbehind, preceded by abc |
(?<!abc) | Negative lookbehind. NOT preceded by abc |
Flags
g | Global, find all matches, not just the first |
i | Case-insensitive |
m | Multiline, ^ and $ match line breaks |
s | Dotall, . matches newlines too |
u | Unicode mode |
y | Sticky, match must start at lastIndex |