Regex Cheatsheet
A quick reference for regular expressions (JavaScript / PCRE-compatible syntax). Try any of these live in the regex tester — it highlights matches and shows your capture groups.
Open the regex tester →
Character classes
| Token | Matches |
. | Any character except newline (with s flag, includes newline) |
\d / \D | A digit [0-9] / a non-digit |
\w / \W | Word char [A-Za-z0-9_] / non-word char |
\s / \S | Whitespace / non-whitespace |
[abc] | Any one of a, b, or c |
[^abc] | Any character except a, b, c |
[a-z0-9] | A range: any lowercase letter or digit |
Anchors & boundaries
| Token | Matches |
^ / $ | Start / end of string (or line, with m flag) |
\b / \B | Word boundary / non-boundary |
Quantifiers
| Token | Matches |
* | 0 or more |
+ | 1 or more |
? | 0 or 1 (optional) |
{3} | Exactly 3 |
{2,} | 2 or more |
{2,5} | Between 2 and 5 |
*? +? ?? | Lazy (as few as possible) versions of the above |
Groups & alternation
| Token | Meaning |
(abc) | Capture group (numbered) |
(?:abc) | Non-capturing group |
(?<name>abc) | Named capture group |
a|b | Match a or b |
\1 | Backreference to group 1 |
Lookarounds
| Token | Meaning |
(?=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
| Flag | Effect |
g | Global — find all matches, not just the first |
i | Case-insensitive |
m | Multiline — ^/$ match line starts/ends |
s | Dotall — . matches newlines |
u | Unicode mode |
y | Sticky — match from lastIndex only |
Common patterns
Starting points — validate against your real data; "perfect" email/URL regexes don't exist.
| Goal | Pattern |
| Integer | -?\d+ |
| Decimal | -?\d+(\.\d+)? |
| Email (pragmatic) | [^\s@]+@[^\s@]+\.[^\s@]+ |
| URL (http/https) | https?:\/\/[^\s]+ |
| Hex color | #[0-9a-fA-F]{6}\b |
| IPv4 (loose) | \b\d{1,3}(\.\d{1,3}){3}\b |
| ISO date | \d{4}-\d{2}-\d{2} |
| Slug | [a-z0-9]+(?:-[a-z0-9]+)* |
Test these in the live regex tester →