Regular expressions pack a lot of meaning into a few characters, and the hardest part of learning them is remembering what each symbol does. This cheat sheet lists every common regex token grouped by purpose — character classes, anchors, quantifiers, groups, and flags — with a plain-English meaning and a short example. It uses the JavaScript (ECMAScript) engine, the same one behind the Regex Tester, so every pattern here behaves identically in JavaScript, TypeScript, and Node.js.
Character classes
Character classes match a single character from a set. A backslash shorthand like \d is the quickest way to match a whole category, while square brackets let you define your own set.
| Token | Matches |
|---|---|
| \d | Any digit, 0–9 |
| \D | Any non-digit |
| \w | Word character: a–z, A–Z, 0–9, _ |
| \W | Any non-word character |
| \s | Whitespace: space, tab, newline |
| \S | Any non-whitespace |
| . | Any character except newline |
| [abc] | a, b, or c |
| [^abc] | Any character except a, b, or c |
| [a-z] | Any lowercase letter (a range) |
Anchors and boundaries
Anchors do not match characters; they match positions. They are how you say 'at the start of the line' or 'at a word boundary' rather than matching a character there.
| Token | Matches at |
|---|---|
| ^ | Start of the string (or line, with the m flag) |
| $ | End of the string (or line, with the m flag) |
| \b | A word boundary — between a word and a non-word character |
| \B | A position that is not a word boundary |
Quantifiers
Quantifiers say how many times the preceding token may repeat. They are greedy by default, matching as much as possible; add a ? to make them lazy and match as little as possible.
| Token | Repeats the preceding |
|---|---|
| * | 0 or more times |
| + | 1 or more times |
| ? | 0 or 1 time (optional) |
| {3} | Exactly 3 times |
| {2,5} | Between 2 and 5 times |
| {2,} | 2 or more times |
| +? | 1 or more, lazily (as few as possible) |
Groups and alternation
Parentheses group part of a pattern so a quantifier can apply to all of it, and they capture the matched text for later use. Alternation, written with a pipe, matches one option or another.
| Token | Meaning |
|---|---|
| (abc) | Capture group — remembers what it matched |
| (?:abc) | Non-capturing group — groups without capturing |
| (?<name>abc) | Named capture group |
| a|b | a or b (alternation) |
| \1 | Backreference to the first capture group |
Flags
Flags change how the whole pattern is applied. They are appended after the closing slash in literal notation, such as /pattern/gi.
| Flag | Effect |
|---|---|
| g | Global — find all matches, not just the first |
| i | Case-insensitive matching |
| m | Multiline — ^ and $ match at line breaks |
| s | dotAll — . also matches newlines |
| u | Unicode mode |
| y | Sticky — match from the exact position only |
Practical patterns
A few ready-to-use patterns for common tasks. Test and adapt them to your exact input in the Regex Tester before relying on them.
Email: [\w.+-]+@[\w-]+\.[\w.-]+
URL: https?://[\w.-]+(?:/[\w./?%&=-]*)?
Date (ISO): (\d{4})-(\d{2})-(\d{2})
Hex color: #[0-9a-fA-F]{6}
US phone: \(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}
Whitespace: \s+Frequently asked questions
What regex flavor does this cheat sheet use?+
The JavaScript (ECMAScript) engine, which is what browsers and Node.js use. Most tokens are shared across flavors, but a few features differ in PCRE, Python, and Java.
What is the difference between greedy and lazy quantifiers?+
Greedy quantifiers match as much text as possible; adding a ? makes them lazy so they match as little as possible. This matters when a pattern could match several lengths.
How do I match a literal special character?+
Escape it with a backslash. For example \. matches a literal dot and \( matches a literal parenthesis.
What is a word boundary?+
\b matches the position between a word character and a non-word character, so \bcat\b matches the word cat but not the cat inside category.