Skip to the content

Regex Tester

Try a regular expression against sample text and see every match and group.

Try:

JavaScript syntax, run by your browser's own RegExp engine.

Cheat sheet

Characters

.
Any character except a line break
\d \D
A digit 0-9, anything but a digit
\w \W
A letter a-z, a digit or an underscore; anything else
\s \S
White space, anything but white space
[abc] [^abc]
One of a, b, c; any character except those
[a-z0-9]
A character in one of the ranges
\p{L}
A letter in any script (needs the u flag)
\. \\ \/
A literal dot, backslash, slash

How many

a*
Zero or more
a+
One or more
a?
Zero or one
a{3}
Exactly three
a{2,5}
Two to five
a{2,}
Two or more
a*? a+? a??
The same, but as few as possible (lazy)

Positions

^
Start of the text, or of a line with the m flag
$
End of the text, or of a line with the m flag
\b
The edge of a word
\B
Anywhere that is not the edge of a word

Groups

(abc)
A group whose text is remembered
(?<name>abc)
The same, with a name
(?:abc)
A group that is not remembered
a|b
Either a or b
\1 \k<name>
The same text a group matched earlier

Looking around

a(?=b)
a, only when b follows
a(?!b)
a, only when b does not follow
(?<=b)a
a, only when b comes before it
(?<!b)a
a, only when b does not come before it

In the replacement

$1 $2
The text of group 1, group 2
$<name>
The text of a named group
$&
The whole match
$` $'
The text before the match, after the match
$$
A dollar sign
Share this tool:TwitterFacebookLinkedInWhatsAppEmail

Which kind of regex is this?

The pattern is handed to your browser's own RegExp engine, so it behaves here as it will in JavaScript and TypeScript code. Other languages are close but not identical; the differences that bite most often are listed further down.

The matches are colored in the text itself, with two colors taking turns so that neighbors can be told apart. An empty match, such as the one \b makes, shows as a thin line. In the list, index is the position where a match starts, counting from 0 as JavaScript does.

The flags

FlagNameWhat it changes
gglobalAll matches are found. Without it the search stops at the first one, and a replacement changes only that one.
iignore casea also matches A.
mmultiline^ and $ match at the start and end of every line, not only of the whole text.
sdot all. also matches a line break.
uunicodeThe pattern works on whole characters, so . matches an emoji as one character, and \p{L} (any letter) and \u{1F600} become available. A meaningless escape such as \a becomes an error.
ystickyA match has to start exactly where the previous one ended. Nothing in between is skipped.
dindicesThe engine also reports where each group starts and ends. The list of matches then shows those positions.

Greedy and lazy

A quantifier takes as much as it can. On the text <b>one</b> and <b>two</b>, the pattern <b>.*</b> gives one match, the whole line, because .* runs to the end and only gives back what it must to find a final </b>. Add a question mark and it takes as little as it can: <b>.*?</b> gives two matches, <b>one</b> and <b>two</b>.

"This pattern takes too long"

Some patterns can match the same text in a huge number of ways, and when the match fails in the end, the engine tries them all. (a+)+$ on thirty letters a followed by an exclamation mark has over 500 million ways to split up the a's. This is called catastrophic backtracking, and on a server it is a way to take a site down with one request.

The pattern runs in a background worker here. If it has not finished after two seconds it is stopped and you get a message, and the page keeps working. The cure is nearly always to stop nesting repeats: a+$ does the same job as (a+)+$.

Coming from Python, PHP or PCRE

  • A named group is (?<name>...). Python's (?P<name>...) is a syntax error in JavaScript.
  • Flags go after the pattern. Writing (?i) at the start of the pattern, as PCRE and Python allow, does not work.
  • There are no possessive quantifiers (a++) and no atomic groups ((?>...)), the usual tools against backtracking elsewhere.
  • \d and \w only know ASCII: \d is 0 to 9, and \w is the letters a to z in both cases, the digits and the underscore. The u flag does not widen them to other scripts. In Python 3 both also match letters and digits from other scripts. For "any letter" use \p{L} with the u flag.
  • Lookbehind, (?<=...) and (?<!...), works in all current browsers. Safari was the last to add it, in version 16.4.

Why not to validate e-mail addresses with a regex

The sample pattern finds things that look like addresses, which is fine for pulling them out of a text. As a check on a form field it will always be wrong somewhere. The rules allow far more than people expect, including quoted names with spaces ("ada lovelace"@example.com) and addresses in other scripts, so a strict pattern turns away real customers. And a pattern can't tell whether the mailbox exists. Check that there is an @ with something on both sides, then send a message with a confirmation link.

Your text stays here

Pattern and text are processed in your browser and are not sent anywhere or stored.

See all 12