oktools

Regex Tester & Explainer

An online regex tester, checker and validator for JavaScript patterns: test a regular expression against your own text, see every match highlighted as you type, check that the pattern compiles, and read back in plain English what it actually does. Nothing leaves your browser.

Start from:

101 characters

Matches

3Matches
2Capture groups
61Characters matched

How it works

The pattern and its flags are compiled with the browser’s own RegExp constructor, so a syntax error is the engine’s complaint with its “Invalid regular expression” prefix removed, and the flag string is rebuilt in the order d g i m s u v y. Once typing pauses for 150 milliseconds the pattern is run over the test string with exec in a loop, as String.matchAll would: without g or y it stops at the first match, a zero-length match is stepped past by hand and the scan ends after 500 matches or 250 milliseconds. A second scan with g added finds out whether the flag would reach more, and the list says so.

The scan runs in a Web Worker rather than on the page. A single match attempt cannot be interrupted, so a pattern that backtracks catastrophically is handled by terminating the worker after one second and starting a fresh one for the next job; the page stays responsive and the result area says why there is nothing to show. Where no worker can be started — an old browser or a strict content security policy — matching runs inline, and a pattern flagged as catastrophic is not run over a test string longer than 2,000 characters. Matches are painted on a layer behind a transparent textarea, so the highlights sit under the real characters.

The explanation comes from a separate parser, which never throws: a half-typed pattern is explained as far as it goes. It walks alternations, sequences and atoms, folds a run of plain characters into one phrase, lists a character class item by item with A–Z and 0–9 named, nests a group over what it holds and turns a quantifier into words. The same walk numbers the capture groups that label the match list and flags an unbounded repeat of a group that already repeats without limit. The replacement preview is String.replace with the same regex, so $1, $<name>, $& and $$ behave as they will in your code.

Examples

Pull addresses out of a log with named groups

Offsets are zero-based start and end positions in the test string, and each group is listed under its name rather than as $1 and $2. Tick d and the list adds where every group started and ended.

Pattern and test string
/(?<user>[\w.+-]+)@(?<host>[\w-]+\.[\w.-]+)/g

Contact ada@example.com or ops@oktools.dev for access.
Matches
#1  ada@example.com  8–23
    user  ada    host  example.com
#2  ops@oktools.dev  27–42
    user  ops    host  oktools.dev

Read an inherited pattern in plain English

The two groups are nested over the pieces they hold, and a quantifier is spelled out on the row it applies to. The flags in use are described under the list, so with m on the two anchors read as the start and end of each line.

Pattern
^(?<year>\d{4})-(\d{2})$
Explanation
^               Start of the text — or of any line when the m flag is on
(?<year>\d{4})  Capture group 1, named “year”
    \d{4}       Any digit (0–9) — repeated exactly 4 times
-               The character “-”
(\d{2})         Capture group 2 — kept so you can reuse it
    \d{2}       Any digit (0–9) — repeated exactly 2 times
$               End of the text — or of any line when the m flag is on

Preview a replacement with named references

Both dates are rewritten because g is on; switch it off and only the first changes, and the preview says so. The result is what text.replace(pattern, replacement) returns in JavaScript, placeholders and all.

Pattern, test string and replacement
/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/g

Released 2024-05-17, patched 2024-11-02.

Replace each match with: $<day>.$<month>.$<year>
Result
Released 17.05.2024, patched 02.11.2024.

Spot a pattern that can hang a server

The warning appears as soon as the pattern compiles, before it runs. Over a short test string it still matches; over a long run of words that ends in a character the pattern cannot match, the worker is stopped at one second and the page says so.

Pattern
(\w+\s?)+$
Warning
This pattern repeats, without limit, a group that already repeats without limit inside. On text that nearly matches, the engine can take a very long time to give up — known as catastrophic backtracking.

Supported formats

  • JavaScript regular expressions, as the browser runs themLookahead and lookbehind, named groups, backreferences by number or \k<name>, \p{…} property escapes under u or v, and the v flag’s set operations inside a class. What compiles is decided by the browser’s engine, which is the one your code will run on.
  • All eight flagsd, g, i, m, s, u, v and y, each explained in place as it is toggled and listed under the explanation while it is on. u and v are never combined, since the engine rejects the pair.
  • Replacement placeholdersEverything String.replace understands: $& for the whole match, $1 to $99 for numbered groups, $<name> for a named one, $$ for a literal dollar sign, and the backtick and quote forms for the text before and after the match.
  • The match listOne row per match with its zero-based start and end, each capture group by name or number, “not matched” for a group in an untaken branch, “empty” for one that matched nothing and “empty match” for a zero-length match. With d, the start and end of every group as well.

Use cases

  • Extract fields from a logIP addresses, timestamps, request IDs, email addresses: the starting points cover the common shapes, named groups label what each one captured, and the match list shows every line the pattern reached and every one it missed.
  • Check a validation pattern before shipping itTry the values a form should accept and the ones it must refuse, in one test string, and see which match. Recent browsers compile an HTML pattern attribute with the v flag, so tick v to test it the way the browser will read it.
  • Work out what an inherited regex doesPaste a pattern from a codebase into the field and read the explanation top to bottom. Each row is one piece, nested under the group that holds it, which is quicker than reading the brackets.
  • Review a pattern for ReDoS before it reaches a serverA pattern that trips the catastrophic warning, or that gets stopped at one second here, will hang a request handler just the same. Rewrite it with a bounded repeat or a non-overlapping shape before deploying it.

Limitations

  • The engine is the browser’sSupport follows the browser version: an older browser may refuse lookbehind or the v flag that a current one accepts, and a pattern that works here may still be rejected by an older Node.js release.
  • The list is cappedMatching stops after 500 matches or 250 milliseconds and the count shows a plus sign, so the tool cannot tell you how many times a pattern occurs in a large file. Use grep -c, or narrow the pattern.
  • One test string, typed or pastedThere is no file upload and no line-by-line mode: the pattern runs over the text as a whole, and the m flag is how ^ and $ are made to work per line. A log of several megabytes is better handled with grep or ripgrep.
  • No translation to other dialectsCopy as JS is the only code export. The same pattern in Python, Go or Java may need different escapes, and RE2-based engines such as Go’s regexp and Rust’s regex crate accept neither lookaround nor backreferences.

Frequently asked questions

Which regex flavour does this test?

JavaScript’s own RegExp engine, exactly as it behaves in a browser and in Node.js. Patterns that rely on PCRE, Python or .NET extensions — possessive quantifiers, atomic groups, recursion, \K — are not supported here because they are not supported in JavaScript either.

What do the d, g, i, m, s, u, v and y flags do?

g finds every match rather than just the first, i ignores case, m makes ^ and $ match at each line rather than only at the ends of the text, s lets . match a newline, u turns on full Unicode handling, v is the newer Unicode-sets mode that also allows set operations inside a character class and replaces u (the engine refuses the two together, so ticking one unticks the other), y anchors matching at the current position and d records where each capture group started and ended, which the match list then shows beside every group. Each flag is explained in place as you toggle it.

Why does my pattern only match once?

Because the g flag is off. Without it the engine stops at the first match — which is also what String.replace and RegExp.exec do — so a pattern that looks correct appears to miss later occurrences. The tool re-runs the pattern with g behind the scenes, and when that would find more it points it out and offers to add the flag for you.

How do I test a replacement?

Switch to the replace view and type the replacement string. Group references such as $1 and named references such as $<year> are substituted as String.replace would substitute them, so what you see is what your code will produce.

Why did matching stop partway through, or not run at all?

A pattern that backtracks catastrophically — typically nested unbounded quantifiers like (a+)+ over a string that nearly matches — can run effectively forever, and a single match attempt cannot be interrupted once it has started. So matching runs in a background worker, apart from the page, 150 milliseconds after you stop typing: if it has not finished within one second the worker is stopped outright and a notice says so, while the page stays responsive. A long list of matches is also cut off between matches once it has used more than 250 milliseconds, and shown as far as it got. In a browser that cannot start a worker, a pattern of that shape is instead not run at all over a test string longer than 2,000 characters. The warning under the pattern is a real finding either way: the same pattern would hang your server, which is how regular-expression denial of service happens.

What does the explanation panel do?

It reads the pattern apart and describes each piece in plain English — what a character class covers, what a quantifier repeats, what a group captures — nested to match the structure. It is the fastest way to work out what an inherited pattern actually does before you change it.

Is the text I test against sent anywhere?

No. The whole tool is JavaScript running in your own browser, so nothing you type or paste is sent over the network, written to a log or attached to an analytics event. You can confirm it by opening the network tab and watching it stay empty while you work.