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
Preview
Matches
Cheatsheet
Common JavaScript regular expression syntax at a glance.
Character classes
.- Any character except newline (with
s, anything).a.cmatches"abc","axc". \d\D- Digit / non-digit.
\d\dmatches"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
uflag): any letter, including"é"and"日". \.- A backslash makes a special character literal:
a\.cmatches"a.c"only.
Quantifiers
*- Zero or more.
ab*cmatches"ac"and"abbc". +- One or more.
ab+cneeds at least one"b". ?- Zero or one.
colou?rmatches"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).^abcrejects"xabc". $- End of string (end of line with
m).\d+$matches the trailing"123". \b- Word boundary.
\bcat\bmatches"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 underyear. \1\k<name>- Backreference to group 1 / to a named group.
(\w+) \1matches"hey hey". a|b- Alternation.
cat|dogmatches either word.
Lookarounds
(?=abc)- Positive lookahead.
\d+(?=px)matches the12in"12px"but not in"12em". (?!abc)- Negative lookahead: only when
abcdoes not follow. (?<=abc)- Positive lookbehind.
(?<=\$)\d+matches the5in"$5". (?<!abc)- Negative lookbehind: only when
abcdoes not precede.
Flags
g- Global: find all matches, not just the first.
i- Ignore case:
/abc/imatches"ABC". m- Multiline:
^and$match every line. s- DotAll:
.also matches newlines. u- Unicode: full code-point matching, enables
\p{…}.