39 entries

Regex Cheat Sheet

Regular expressions describe patterns in text. The syntax divides into character classes (what to match), quantifiers (how many), anchors (where), groups (what to capture) and flags (how the whole pattern behaves). This reference uses JavaScript syntax, which is what runs in a browser and is close to PCRE for everyday patterns.

Almost all practical regex work uses a small subset of the syntax. The tables below are grouped by intent rather than alphabetically, because you generally know what you are trying to express and need the notation for it.

One warning worth reading before the tables: nesting a quantifier inside another quantified group, as in (a+)+, can make matching take exponential time. On a few dozen characters that is minutes of pegged CPU. It is the single most dangerous thing in this reference.

Character classes

11 entries

What a single position may match.

Pattern Matches Example
. Any character except a newline a.c matches abc, a-c
\d A digit, 0–9 \d\d matches 42
\D Anything that is not a digit \D+ matches abc
\w Word character: letter, digit or underscore \w+ matches user_1
\W Anything that is not a word character \W matches a space or !
\s Whitespace: space, tab, newline \s+ collapses runs of space
\S Anything that is not whitespace \S+ matches one token
[abc] Any one of a, b or c [aeiou] matches a vowel
[^abc] Any character except a, b or c [^0-9] matches a non-digit
[a-z] A range [A-Za-z0-9] matches alphanumerics
\. A literal dot example\.com

Quantifiers

8 entries

How many times the preceding item may repeat.

Pattern Matches Example
* Zero or more ab*c matches ac, abc, abbc
+ One or more ab+c matches abc but not ac
? Zero or one — optional colou?r matches both spellings
{3} Exactly three \d{4} matches a year
{2,} Two or more \d{2,} matches 42 and 4242
{2,4} Between two and four \w{2,4}
*? Lazy: as few as possible \[(.*?)\] stops at the first ]
+? Lazy one or more <.+?> matches one tag, not the whole line

Anchors and boundaries

4 entries

Where in the text a match may begin or end.

Pattern Matches Example
^ Start of string, or of a line with the m flag ^Error matches a line starting with Error
$ End of string, or of a line with the m flag \.$ matches a trailing full stop
\b A word boundary \bcat\b matches cat but not category
\B Not a word boundary \Bcat matches the cat inside bobcat

Groups and alternation

6 entries

Capturing parts of a match, and matching alternatives.

Pattern Meaning Example
(abc) Capturing group, numbered from 1 (\d{4})-(\d{2}) captures year and month
(?:abc) Non-capturing group Use when you only need a quantifier
(?&lt;name&gt;abc) Named capturing group Referenced as groups.name
a|b Either a or b cat|dog matches either word
\1 Backreference to group 1 (\w)\1 matches a doubled letter
$1 Group 1 in a replacement Swap with $2 $1

Lookaround

4 entries

Assert what is next to a match without consuming it.

Pattern Meaning Example
(?=abc) Positive lookahead — followed by \d+(?= items) matches the number only
(?!abc) Negative lookahead — not followed by foo(?!bar) matches foo unless bar follows
(?&lt;=abc) Positive lookbehind — preceded by (?<=\$)\d+ matches the amount after $
(?&lt;!abc) Negative lookbehind — not preceded by (?<!\$)\d+

Flags

6 entries

Applied to the whole pattern.

Flag Meaning Notes
g Global — find every match Without it, matching stops at the first result
i Case-insensitive Applies to the whole pattern
m Multiline Makes ^ and $ match at line boundaries
s Dot matches newlines Sometimes called dotall
u Unicode mode Required for \u{…} and \p{…} escapes
y Sticky Matches only from lastIndex

Notes

  • This is JavaScript regex syntax, which is what runs in a browser. It is close to PCRE for everyday patterns but has no atomic groups and no possessive quantifiers, and lookbehind support depends on the engine version.
  • Avoid nesting quantifiers. A pattern like (a+)+$ can force exponential backtracking, taking minutes on a few dozen characters. Rewrite so each position can only be matched one way.
  • Do not parse HTML with a regex. Nesting is not a regular language, so a pattern that works on your sample will break on nested or malformed markup. Extracting one attribute from known-shape markup is fine.

Frequently asked questions

What is the difference between greedy and lazy?
A greedy quantifier such as .* takes as much as it can and then gives characters back until the rest of the pattern matches, so it runs to the last possible match on the line. A lazy one such as .*? takes as little as possible, stopping at the first match — which is nearly always what you want between two delimiters.
When should I use a non-capturing group?
Whenever you need parentheses only to apply a quantifier or an alternation, not to capture. It keeps your group numbering stable and is marginally faster, and it makes the intent obvious to whoever reads the pattern next.
Why does my pattern hang the browser?
Almost certainly catastrophic backtracking from nested quantifiers. Look for a quantified group that is itself quantified, such as (\w+)*. Restructure so there is only one way for the engine to split the input.
Does \d match non-Latin digits?
In JavaScript, \d matches only 0–9 regardless of flags. To match digits in other scripts you need Unicode property escapes: \p{Nd} with the u flag.

Related tools

All tools →
All references →

Last reviewed .