TLDR reference
Regex cheatsheet
A searchable reference for regular-expression syntax, each token with its meaning and an example. Type to search by symbol or description, or filter by group. Everything runs in your browser.
38 shown
-
^
AnchorsStart of the string (or of a line with the m flag).
^Hello matches "Hello" only at the very start.
-
$
AnchorsEnd of the string (or of a line with the m flag).
end$ matches "end" only at the very end.
-
\b
AnchorsA word boundary: the edge between a word and a non-word character.
\bcat\b matches "cat" but not the "cat" in "category".
-
\B
AnchorsA non-word boundary: anywhere that is not a word edge.
\Bcat matches "cat" inside "concatenate".
-
.
Character classesAny single character except a line break (any character with the s flag).
a.c matches "abc", "a-c", "a c".
-
\d \D
Character classesA digit 0-9, or its negation: any non-digit.
\d\d matches "42"; \D matches "a".
-
\w \W
Character classesA word character (letter, digit, underscore), or its negation.
\w+ matches "user_1".
-
\s \S
Character classesA whitespace character, or its negation: any non-whitespace.
a\sb matches "a b".
-
[abc]
Character classesAny one of the listed characters.
[aeiou] matches a single vowel.
-
[^abc]
Character classesAny character except the listed ones (a negated class).
[^0-9] matches any non-digit.
-
[a-z]
Character classesAny character in the range. Combine ranges freely.
[A-Za-z0-9] matches one alphanumeric character.
-
*
QuantifiersZero or more of the preceding token (greedy).
ab*c matches "ac", "abc", "abbbc".
-
+
QuantifiersOne or more of the preceding token (greedy).
\d+ matches "1" or "1000".
-
?
QuantifiersZero or one: makes the preceding token optional.
colou?r matches "color" and "colour".
-
{n}
QuantifiersExactly n repetitions.
\d{4} matches a four-digit year.
-
{n,}
QuantifiersAt least n repetitions.
\d{2,} matches "12" or longer.
-
{n,m}
QuantifiersBetween n and m repetitions, inclusive.
\d{2,4} matches 2 to 4 digits.
-
*? +? ??
QuantifiersLazy quantifiers: match as few characters as possible.
<.*?> matches "<a>" rather than the whole "<a><b>".
-
(...)
Groups & lookaroundA capturing group: groups tokens and remembers what matched.
(ab)+ matches "abab"; group 1 captures "ab".
-
(?:...)
Groups & lookaroundA non-capturing group: groups without creating a numbered capture.
(?:ab)+ groups but captures nothing.
-
(?<name>...)
Groups & lookaroundA named capturing group, referenced by name later.
(?<year>\d{4}) captures into "year".
-
a|b
Groups & lookaroundAlternation: match the expression on either side.
cat|dog matches "cat" or "dog".
-
(?=...)
Groups & lookaroundA positive lookahead: assert what follows, without consuming it.
\d+(?= USD) matches the digits before " USD".
-
(?!...)
Groups & lookaroundA negative lookahead: assert what does NOT follow.
\d+(?! px) matches digits not followed by " px".
-
(?<=...)
Groups & lookaroundA positive lookbehind: assert what precedes the match.
(?<=\$)\d+ matches digits after a "$".
-
(?<!...)
Groups & lookaroundA negative lookbehind: assert what does NOT precede.
(?<!\$)\d+ matches digits not after a "$".
-
\1
Groups & lookaroundA backreference: match the same text a numbered group captured.
(\w)\1 matches a doubled letter, as in "letter".
-
g
FlagsGlobal: find all matches, not just the first.
/a/g finds every "a".
-
i
FlagsCase-insensitive matching.
/hello/i matches "Hello".
-
m
FlagsMultiline: ^ and $ match at every line break.
/^line/m matches at the start of each line.
-
s
FlagsDotAll: the dot also matches line breaks.
/a.b/s matches across a newline.
-
u
FlagsUnicode: handle code points and \u{...} correctly.
/\u{1F600}/u matches an emoji.
-
y
FlagsSticky: match only from the regex lastIndex position.
Used for tokenizers that scan left to right.
-
\. \\ \*
EscapesEscape a metacharacter to match it literally.
\. matches a real dot, not any character.
-
\t \n \r
EscapesTab, newline, and carriage return.
\n matches a line break.
-
\uFFFF
EscapesA character by its four-digit hexadecimal code unit.
\u00e9 matches "e" with an acute accent.
-
\u{1F600}
EscapesA code point by hex value (requires the u flag).
\u{1F600} matches a grinning-face emoji.
-
\xFF
EscapesA character by its two-digit hexadecimal code.
\x41 matches "A".
No tokens match your search.
How it works
A searchable cheatsheet for regular expressions, with every token paired with a plain-language meaning and a small example. Regex packs a lot of power into a few symbols, and the hard part is remembering which symbol does what: this groups them the way you think about them - anchors, character classes, quantifiers, groups and lookaround, flags, and escapes - so you can find the piece you need without scrolling a wall of syntax. Type to search by symbol or by description, such as "word boundary" or "lazy".
The syntax follows the JavaScript flavour (the same engine behind the regex tester on this site), which is close to what PCRE, Python, and most other languages use day to day. Each card shows the token, what it matches, and an example so an abstract symbol like \b or (?=...) becomes concrete. Everything is static and runs in your browser, so the lookup is instant and works offline once the page has loaded.
Example. Searching "repeat" surfaces the quantifiers together: + means one or more, * means zero or more, ? makes the preceding token optional, and {2,4} matches between two and four times. Filtering by the Groups & lookaround chip lines up (?:...) non-capturing groups next to (?=...) lookahead and (?<!...) negative lookbehind so you can see how they differ.
FAQ
What is the difference between greedy and lazy quantifiers?
By default quantifiers are greedy: they match as much text as they can and then give characters back only if the rest of the pattern fails. Adding a ? after a quantifier makes it lazy, so it matches as little as possible and expands only when forced to. On the string <a><b>, the greedy pattern <.*> matches the whole thing in one go, while the lazy <.*?> matches just <a>. Lazy quantifiers are the usual fix when a pattern is grabbing more than you intended.
What does a word boundary (\b) actually match?
A word boundary is a zero-width assertion: it matches the empty position between a word character (a letter, digit, or underscore) and a non-word character, or at the start or end of the string. It does not consume any characters; it just anchors the match. That is what lets \bcat\b match the word "cat" but not the "cat" inside "category" or "concatenate", because in those words the boundary on at least one side is missing.
What is the difference between a capturing and a non-capturing group?
Plain parentheses (...) do two jobs at once: they group part of the pattern so a quantifier or alternation applies to the whole thing, and they capture the matched text so you can reuse it via a backreference or in a replacement. When you only need the grouping and not the captured value, use a non-capturing group (?:...). It groups without numbering, which keeps your capture groups uncluttered and is slightly faster. Named groups (?<name>...) capture too, but let you refer to the result by name instead of by number.
How do I match a literal special character like a dot or a slash?
Escape it with a backslash. A bare dot . matches any character, so to match a real full stop you write \. - likewise \* for a literal asterisk, \( for a literal opening bracket, and \\ for a literal backslash. Inside a character class many of these lose their special meaning, so [.] also matches a literal dot. When in doubt, escaping a punctuation character is harmless, so reaching for the backslash is a safe habit.