Regex Tester

Test JavaScript regular expressions against any text. Matches highlighted in real-time.

//
Contact us at hello@example.com or support@example.org for help. Also try info@anywhere.com

Patterns with nested repeats such as (a+)+ or (.*)* can take exponentially long on text that almost matches, which can freeze this tab. The regex runs in your browser, so if the page stops responding, close the tab and simplify the pattern.

What regular expressions do

A regular expression is a pattern that describes a set of strings. Given a pattern and some text, a regex engine finds every part of the text matching that pattern - which makes regex the standard tool for searching, validating, extracting and replacing text.

They have a well-earned reputation for being hard to read. Building one incrementally against real test data, which is what this tester is for, is considerably more productive than writing one in your head and hoping.

Key takeaways

  • Build patterns incrementally and test against real data as you go.
  • Quantifiers are greedy by default - add ? to make them lazy.
  • Anchors ^ and $ change meaning with the multiline flag.
  • Escape characters that have special meaning if you mean them literally.
  • Do not parse HTML with regex; use a real parser.

Core syntax

PatternMatches
.Any character except a newline
\dAny digit, 0-9
\wAny word character: letter, digit or underscore
\sAny whitespace
\D \W \SThe negation of each of the above
[abc]Any one of a, b or c
[^abc]Any character except a, b or c
[a-z]Any character in the range
^Start of string, or of a line with the m flag
$End of string, or of a line with the m flag
\bA word boundary

Quantifiers

QuantifierMeaningExample
*Zero or moreab*c matches ac, abc, abbc
+One or moreab+c matches abc, abbc, not ac
?Zero or onecolou?r matches color and color
{n}Exactly n\d{4} matches a four-digit year
{n,}n or more\d{3,} matches three or more digits
{n,m}Between n and m\d{2,4} matches two to four digits
*? +? ??The lazy versionsMatch as little as possible
Greedy versus lazy, the classic example. Against <a><b>, the pattern <.*> matches the whole string, because * takes as much as it can and then backtracks only enough to let the final > match. The lazy version <.*?> matches just <a>. This single behavior accounts for a large share of confusing regex results.

Groups and references

SyntaxMeaning
(abc)Capturing group - the match is kept
(?:abc)Non-capturing group - grouping only
(?<name>abc)Named capturing group
\1Backreference to the first captured group
$1 or \1Reference to a group in the replacement string
a|bAlternation - matches a or b

Use non-capturing groups when you only need grouping for alternation or quantification. It keeps the capture numbering clean and is marginally faster.

Lookarounds

SyntaxNameMatches
(?=abc)Positive lookaheadA position followed by abc
(?!abc)Negative lookaheadA position not followed by abc
(?<=abc)Positive lookbehindA position preceded by abc
(?<!abc)Negative lookbehindA position not preceded by abc

Lookarounds are zero-width: they assert a condition without consuming characters, so the matched text does not include them.

A common use is password validation - (?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,} requires at least one digit, one lowercase and one uppercase letter in a string of eight or more characters. Though as the password generator page argues, composition rules of this kind are poor security practice.

Flags

FlagNameEffect
gGlobalFind all matches, not just the first
iIgnore caseCase-insensitive matching
mMultiline^ and $ match at line boundaries
sDot all. also matches newlines
uUnicodeEnables full Unicode and \p{...} property escapes
yStickyMatch only from the exact lastIndex position

Catastrophic backtracking

A badly constructed regex can hang your application. Patterns with nested quantifiers over overlapping character sets - (a+)+b is the textbook example - can take exponential time on input that nearly matches but ultimately does not. A 30-character string can take years to evaluate. This is a real denial-of-service vector known as ReDoS, and it has taken down production systems.
  • Avoid nested quantifiers such as (x+)+ or (a|a)*.
  • Make character classes mutually exclusive so the engine has no ambiguous choices to backtrack through.
  • Anchor patterns where possible to limit the search space.
  • Set a timeout when running regex against user-supplied input.
  • Prefer a parser for structured formats rather than an elaborate regex.

When not to use regex

  • HTML and XML. They are nested and recursive; regex fundamentally cannot handle arbitrary nesting. Use a parser.
  • JSON. Same reason - use a JSON parser.
  • Email validation. The fully correct pattern is enormous and still does not prove an address exists. Check for an @ with something either side and send a confirmation.
  • CSV with quoted fields, where the delimiter can appear inside data.
  • Anything you can do with simple string functions. A regex for something startsWith handles is harder to read and slower.
Everything runs in your browser. Your data is processed entirely on your own device - nothing is uploaded, logged or stored. That matters when the payload you are debugging contains customer records, API responses or anything else you should not be pasting into a remote service.

Frequently Asked Questions

What is a regular expression?

A pattern describing a set of strings. A regex engine takes the pattern and some text and finds every part of the text that matches, which makes it the standard tool for searching, validating, extracting and replacing text.

What is the difference between greedy and lazy quantifiers?

Greedy quantifiers match as much as possible then backtrack; lazy ones, written with a trailing question mark, match as little as possible. Against "<a><b>", the pattern <.*> matches everything while <.*?> matches only the first tag.

What does the global flag do?

It makes the engine find every match rather than stopping at the first. Without it, methods like replace change only the first occurrence, which is a frequent source of confusion.

What is a non-capturing group?

A group written (?:...) that provides grouping for alternation or quantification without storing the match. It keeps your capture group numbering clean and avoids the small cost of capturing text you do not need.

What are lookaheads and lookbehinds?

Zero-width assertions that check whether a position is followed or preceded by a pattern, without consuming any characters. They are useful for conditions such as "a digit not preceded by a currency symbol", or for requiring several character types in a password.

Why should I not parse HTML with regex?

Because HTML is nested and arbitrarily recursive, and regular expressions cannot express arbitrary nesting - it is a fundamental limitation, not a matter of writing a cleverer pattern. Use a proper HTML parser, which every language has.

What is catastrophic backtracking?

A performance failure where a pattern with nested quantifiers over overlapping character sets takes exponential time on input that nearly matches. A 30-character string can take effectively forever. It is a genuine denial-of-service vector called ReDoS and has brought down production systems.

How should I validate an email address?

Loosely. Check there is an @ with something either side and a dot in the domain, then send a confirmation email. The fully RFC-compliant pattern is enormous, rejects some valid addresses, and still cannot tell you whether the mailbox exists.

Is my pattern or test data sent anywhere?

No. Everything runs in your browser using the JavaScript regex engine. Nothing is transmitted or stored.

Related Calculators