viewer.csslab.dev

Regex Tester

Test a regular expression as you type, see every match and group, and read the pattern back in words.

What is a regular expression?

A regular expression is a small pattern language for describing sets of strings: which characters may appear, in what order, and how many times. A single expression can validate an input, pull fields out of a log line, or rewrite text — which is why regular expressions appear in nearly every language, editor, and command-line tool.

The difficulty is that a regular expression is written as one dense line with no room for a comment, so a pattern that works is often a pattern nobody can safely change six months later. This tester runs the expression as you type, shows every match with its position and capture groups, previews a replacement, and breaks the pattern into tokens with an explanation for each.

A Regular Expression (regexp) is a pattern which specifies a set of character strings.
POSIX.1-2024, Regular Expressions

Features

Reads the pattern back in words
The expression is split into tokens and each one is labelled — \d becomes "any digit 0-9", {3} becomes "repeated exactly n times". This is the part that turns a pattern you copied into a pattern you can edit.
Every match, with position and groups
Matches are listed with their index in the string and the contents of each capture group, so a group that silently captured the wrong thing is visible rather than inferred.
Replacement preview
Enter a replacement — including $1 backreferences — and see the rewritten text immediately, before running it against anything real.
Honest about the dialect
It runs the JavaScript engine, the same one your browser and Node use, and says so. A tester that pretends to support PCRE while quietly behaving differently is worse than no tester at all.
Safe with real data
Patterns and test strings are evaluated in your browser. Log lines and customer records can be used as test input without sending them anywhere.

How to use

  1. 1

    Write the pattern

    Type the expression between the slashes, or start from one of the common patterns.

  2. 2

    Pick the flags

    g finds every match rather than the first, i ignores case, m makes ^ and $ match each line.

  3. 3

    Paste the text to test against

    Matches update as you type, each with its position and capture groups.

  4. 4

    Read the explanation

    The token breakdown says what each part of the pattern does, which is how you check that it means what you intended.

Catastrophic backtracking, and how to see it coming

A pattern like (a+)+$ run against a long string of a's that ends in b does not fail quickly — it tries an exponential number of ways to split the a's before giving up. Thirty characters can take longer than the age of the universe. The shape to watch for is a quantifier inside a group that is itself quantified, especially with alternation that can match the same text two ways.

JavaScript has no possessive quantifiers or atomic groups, so the fix is structural rather than a flag: anchor the pattern so there is only one place it can start, replace nesting with a single character class ([a-z]+ instead of (\w|\d)+), and make alternatives mutually exclusive. When a pattern will run on input you do not control, this is not a performance detail — it is the ReDoS denial-of-service class, and the input is chosen by the attacker.

Frequently asked questions

What do the flags g, i and m do?

g (global) finds every match instead of stopping at the first. i makes matching case-insensitive. m (multiline) makes ^ and $ match at the start and end of each line rather than only the whole string. s makes . match a newline as well, and u enables full Unicode handling.

Why does my regular expression only find the first match?

The g flag is missing. Without it the engine stops at the first match, which is also why methods like replace() only replace once. Turn on g and every occurrence is found.

What is a capture group?

Parentheses around part of a pattern capture whatever that part matched, so you can read it back separately or reference it in a replacement as $1, $2 and so on. Use (?:...) when you want to group something for repetition without capturing it.

Does this support PCRE or Python regular expressions?

No — it runs the JavaScript engine. The syntax overlaps heavily, but there are real differences: lookbehind support, named group syntax, and some Unicode property escapes vary between dialects. Test in the same engine your code will use for anything subtle.

Why is my regular expression extremely slow?

Usually catastrophic backtracking: nested quantifiers such as (a+)+ on a non-matching string force the engine to try an exponential number of paths. Making the inner quantifier possessive is not available in JavaScript, so the fix is to restructure the pattern — anchor it, or replace the nesting with a character class.

Is a regular expression the right tool for validating an email address?

For a rough check, yes — a pattern that requires an @ with something either side catches most typos. For correctness, no: the address grammar in RFC 5322 permits quoted strings and comments that no readable pattern covers, and the only real proof that an address works is sending mail to it.

Specifications this follows

Related tools