Skip to content
DDevToolery

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

TokenMatches
.Any character except a line break — unless the s flag is set
\d \DA digit / anything but a digit
\w \WA word character [A-Za-z0-9_] / anything else
\s \SWhitespace / 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.

TokenMatches
* + ?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

TokenMatches
^ $Start / end of the string, or of each line with the m flag
\b \BA word boundary / not a word boundary

Groups

TokenMatches
(...)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.