Skip to main content
Quick Dev Tools

Regex tool

Test a JavaScript regular expression against sample text with live highlighting, and keep the syntax cheatsheet at hand. Everything happens locally in your browser.

Tester

Flags

Preview

Matches

Cheatsheet

Common JavaScript regular expression syntax at a glance.

Character classes

.
Any character except newline (with s, anything). a.c matches "abc", "axc".
\d \D
Digit / non-digit. \d\d matches "42".
\w \W
Word character [A-Za-z0-9_] / non-word character.
\s \S
Whitespace / non-whitespace. \S+ matches each word.
[abc] [^abc]
One of the listed characters / any character not listed. [a-z0-9] is a range.
\p{L}
Unicode property escape (needs the u flag): any letter, including "é" and "日".
\.
A backslash makes a special character literal: a\.c matches "a.c" only.

Quantifiers

*
Zero or more. ab*c matches "ac" and "abbc".
+
One or more. ab+c needs at least one "b".
?
Zero or one. colou?r matches "color" and "colour".
{3} {2,5} {2,}
Exactly 3 / between 2 and 5 / at least 2 repetitions. \d{3} matches "123".
*? +? {2,5}?
Lazy versions: match as little as possible. <.+?> matches "<a>" in "<a><b>".

Anchors & boundaries

^
Start of string (start of line with m). ^abc rejects "xabc".
$
End of string (end of line with m). \d+$ matches the trailing "123".
\b
Word boundary. \bcat\b matches "a cat", not "concat".
\B
Non-word boundary: the opposite of \b.

Groups & backreferences

(abc)
Capturing group. (ab)+ matches "abab" and remembers it as $1.
(?:abc)
Non-capturing group: group without remembering.
(?<name>abc)
Named group. (?<year>\d{4}) stores the year under year.
\1 \k<name>
Backreference to group 1 / to a named group. (\w+) \1 matches "hey hey".
a|b
Alternation. cat|dog matches either word.

Lookarounds

(?=abc)
Positive lookahead. \d+(?=px) matches the 12 in "12px" but not in "12em".
(?!abc)
Negative lookahead: only when abc does not follow.
(?<=abc)
Positive lookbehind. (?<=\$)\d+ matches the 5 in "$5".
(?<!abc)
Negative lookbehind: only when abc does not precede.

Flags

g
Global: find all matches, not just the first.
i
Ignore case: /abc/i matches "ABC".
m
Multiline: ^ and $ match every line.
s
DotAll: . also matches newlines.
u
Unicode: full code-point matching, enables \p{…}.