regex.reez.cc

regex --cheatsheet

Regex cheatsheet

The syntax of JavaScript regular expressions on one page. Every example was run when this page was built, and the highlighted part is what the pattern really matches. Click an example to open it in the tester.

Characters

TokenMeaningExample
.Any character except a line break (with the s flag: any character)/h.t/ghat hot h t hit
\dA digit, 0 to 9/\d/gRoom 404
\DAnything that is not a digit/\D+/g404 not found
\wA word character: letter, digit or underscore/\w+/gsnake_case, kebab-case
\WAnything that is not a word character/\W+/ga, b; c
\sWhitespace: space, tab, line break/\s+/ga b c
\SAnything that is not whitespace/\S+/gtwo words
[abc]One of the listed characters/gr[ae]y/ggray grey groy
[^abc]Any character that is not listed/[^aeiou ]/gregex tool
[a-z]A character in the range/[a-f0-9]+/gid: 00ff41 xyz
\.A literal dot. The backslash turns off the special meaning of . * + ? ( ) [ ] { } | ^ $ \/\./greez.cc
\t \n \rTab, line feed, carriage return/\t/ga b
\x41The character with this hex code (two digits)/\x41/gAbA
\u00e9The character with this code point (four digits, or \u{1F600} with the u flag)/\u00e9/gcafé
\p{L}A character with a Unicode property, here: any letter. Needs the u flag/\p{L}+/gunaïve Köln 東京 42

Anchors and boundaries

Anchors match a position, not a character.

TokenMeaningExample
^Start of the text (with the m flag: start of every line)/^\w+/gfirst word only
$End of the text (with the m flag: end of every line)/\w+$/gonly the last
\bWord boundary: between a word character and anything else/\bcat\b/gcat concat cats
\BNot a word boundary/\Bcat/gcat concat cats

Quantifiers

A quantifier repeats the part right before it. Greedy takes as much as possible, lazy as little as possible.

TokenMeaningExample
*Zero or more times/ab*/ga ab abbb
+One or more times/ab+/ga ab abbb
?Zero or one time (optional)/colou?r/gcolor colour
{3}Exactly three times/\d{3}/g12 345 6789
{2,}Two or more times/o{2,}/gso soo sooo
{2,4}Two to four times/\d{2,4}/g1 22 4444 55555
*? +? ??Lazy: as few characters as possible/<.+?>/g<b>bold</b>
.*Greedy by default: runs to the last possible match/<.+>/g<b>bold</b>

Groups and references

TokenMeaningExample
(abc)Capturing group: remembered as $1, $2, … in order of the opening bracket/(\d+)px/gwidth: 120px
(?:abc)Non-capturing group: groups without remembering/(?:ha)+/ghahaha ha
(?<name>abc)Named group: remembered as $<name>/(?<user>\w+)@(?<host>\w+)/glukas@reez
a|bAlternation: either side/cat|dog/gcat, dog, bird
\1Backreference: the same text group 1 matched/(\w)\1/ghello bookkeeper
\k<name>Backreference to a named group/(?<q>['"]).*?\k<q>/gsay 'hi' or "bye"

Lookaround

Lookarounds check what is next to the match without making it part of the match.

TokenMeaningExample
(?=abc)Lookahead: followed by abc/\d+(?=px)/g12px 3em 40px
(?!abc)Negative lookahead: not followed by abc/\d+(?!\d|px)/g12px 3 40
(?<=abc)Lookbehind: preceded by abc/(?<=\$)\d+/g$40 and 15
(?<!abc)Negative lookbehind: not preceded by abc/(?<![$\d])\d+/g$40 and 15

Replacement patterns

Used in the replacement text of String.replace and in the Replace tab of the tester.

TokenMeaningExample
$1Text of group 1/(\w+) (\w+)/ghello world → world hello with $2 $1
$<name>Text of a named group/(?<d>\d\d)\.(?<m>\d\d)\./g14.11. → 11/14 with $<m>/$<d>
$&The whole match/\d+/g3 apples, 12 pears → [3] apples, [12] pears with [$&]
$`Everything before the match/b/abc → a(a)c with ($`)
$'Everything after the match/b/abc → a(c)c with ($')
$$A literal dollar sign/USD /gUSD 40 → $40 with $$

Flags

Flags follow the closing slash, as in /pattern/gi, and change how the whole pattern works.

FlagMeaning
gGlobal: find all matches instead of stopping at the first
iIgnore case: a also matches A
mMultiline: ^ and $ match at the start and end of every line
sDot all: . also matches line breaks
uUnicode: treats the pattern as code points, enables \p{…} and \u{…}, rejects unknown escapes
ySticky: the match must start exactly at lastIndex
dIndices: also report start and end of every group. The tester always sets it to show positions

More