Regex cheat sheet
JavaScript flavour. Most of this is portable, but lookbehind, named groups and \p{...} are not universally supported in older engines.
16 entries
Character classes
| Token | Matches | |
|---|---|---|
| . | Any character except a line break — unless the s flag is set | |
| \d \D | A digit / anything but a digit | |
| \w \W | A word character [A-Za-z0-9_] / anything else | |
| \s \S | Whitespace / non-whitespace | |
| [abc] [^abc] | One of these / none of these | |
| \p{L} | Any Unicode letter. Requires the u flag. |
Quantifiers
Add ? after any quantifier to make it lazy — it will match as little as possible.
| Token | Matches | |
|---|---|---|
| * + ? | Zero or more, one or more, zero or one | |
| {3} {3,} {3,5} | Exactly, at least, between | |
| *? +? | Lazy versions — stop at the first opportunity |
Anchors and boundaries
| Token | Matches | |
|---|---|---|
| ^ $ | Start / end of the string, or of each line with the m flag | |
| \b \B | A word boundary / not a word boundary |
Groups
| Token | Matches | |
|---|---|---|
| (...) | Capturing group, referenced as $1 | |
| (?:...) | Grouping without capturing — cheaper | |
| (?<name>...) | Named group, referenced as $<name> | |
| (?=...) (?!...) | Lookahead: followed by / not followed by | |
| (?<=...) (?<!...) | Lookbehind: preceded by / not preceded by |
Nested quantifiers such as (a+)+ can take exponential time on a non-matching input. If a pattern touches untrusted text, keep it simple.