ToolBench

Developer

Test a regular expression

Matches are highlighted in place and listed with their offsets, so you can tell instantly whether the pattern is matching what you meant or merely what you wrote.

/ /

How to use it

  1. Enter the pattern without the surrounding slashes.
  2. Set flags in the small box: g for all, i for case-insensitive, m for multiline.
  3. Paste the text to test against.
  4. Read the match list — each row shows the groups and the character offset.

Catastrophic backtracking

A pattern such as (a+)+$ tested against thirty a's followed by a b takes exponential time, because the engine tries every way of dividing the a's between the two quantifiers before concluding there is no match. This is how one request can pin a CPU at 100% — the technique is called ReDoS and it is a real denial-of-service vector. Nested quantifiers and ambiguous alternations are the usual causes.

Write patterns a human can read

Prefer explicit character classes to bare dots, name your groups where the syntax allows it, and break long patterns across lines with the extended flag. A pattern that took an hour to write will take a day to debug six months later, and that cost lands on whoever inherits the code — usually not you.

Building a pattern one piece at a time

Write the smallest pattern that matches one known-good example, confirm it, then add the next constraint. Patterns written in one go fail in ways that are hard to attribute: a misplaced quantifier and a wrong character class both produce "no match" with no explanation.

Two habits save the most time. Anchor with ^ and $ if you mean the whole string — an unanchored email pattern happily matches an email buried inside 400 lines of prose. And test against a string that must not match: a pattern that only ever sees valid input is not tested, it is decorated.

Frequently asked questions

Why is the flag box prefilled with g?

Because testing a pattern usually means finding every match. Remove it if you only want the first one; the tool adds g internally so highlighting works either way.

Does it support lookbehind?

Yes, in any browser that implements ES2018 lookbehind — current Chrome, Firefox and Safari all do. An unsupported syntax shows an error from the engine instead of failing silently.

What are the $1, $2 labels in the results?

They are capture groups in the order their opening brackets appear. Use them in a replacement string to rearrange the matched text.

Is there a limit on matches?

The highlighter stops after 5,000 iterations to keep the page responsive; the first 60 matches are listed in the table. That is far beyond normal testing needs.

Why does my pattern work in one language but not another?

Dialects differ: JavaScript has no possessive quantifiers or atomic groups, Python supports them, and lookbehind is fixed-width in JavaScript but not in Python or PCRE.

Is there a safe limit on input size?

There is no universal number, but inputs above a few hundred kilobytes plus a nested quantifier is the combination that freezes tabs. Cap the input, not the pattern.