Converter tool

Regex Tester and Explainer

Write a JavaScript regular expression, watch it match your sample text, read each capture group, preview a replacement, and get the pattern spelled out piece by piece.

Flags

0.1 KB of 100 KB

Some patterns, such as nested repeats like (a+)+ run against a long line that almost matches, make a JavaScript regular expression try a huge number of paths. The page cannot interrupt that: the tab may stop responding until the browser offers to close it. If that happens, reload and use a shorter sample text, then tighten the pattern.

Preview

Output Preview

Matching...

Regex Tester and Explainer

Operator: Abdulla Al Fahad. How tools are built and checked

A regular expression that looked right in your head grabs half a line too much, or nothing at all, and the only way to find out why is to run it against real text and look. This page does that while you type. Each match lights up in the sample, a table lists every match with its position and capture groups, and a replace box previews exactly what String.replace would produce with your $1 or $<name> references.

Under the results, the pattern is broken into its pieces with a sentence for each: "a digit 0 to 9", "repeat the previous item exactly 4 times", "start of capturing group 1". The explainer covers the parts of JavaScript regex syntax people use most. When it meets something outside that set, such as a Unicode property escape in non-Unicode mode or an inline modifier group, it labels that piece "not explained" instead of inventing a meaning.

Key features

  • Matches update shortly after you stop typing
  • Highlighting drawn as page elements, with alternating colours so neighbouring matches stay distinct
  • Match table with match number, start index, full match, every numbered group and every named group
  • Groups that did not take part in a match marked "no match", separate from groups that matched empty text
  • Replace preview using the same substitution rules as String.prototype.replace
  • Line-by-line explanation of the pattern, indented by group depth
  • The browser's own error message when a pattern does not compile
  • Up to 1,000 matches listed and highlighted, with the full count still reported

Patterns, flags and replacements follow JavaScript

Patterns are JavaScript regular expressions, compiled by your browser's own RegExp engine, so what matches here matches the same way in the same browser's JavaScript. Other flavours differ: PCRE, Python's re and .NET accept syntax that JavaScript rejects or reads differently, such as the \A and \Z anchors, which JavaScript does not have.

Five flags can be switched on: g (find every match rather than the first), i (ignore case), m (^ and $ match at line breaks), s (. also matches line breaks) and u (Unicode mode, which enables \u{...} and \p{...} and makes the syntax stricter). The sample text can be up to 100 KB, typed, pasted or opened from a .txt file.

The replacement uses the browser's own rules: $1 to $99 insert a numbered group, $<name> a named group, $& the whole match, $` the text before the match, $' the text after it, and $$ a literal dollar sign.

How it works

After a short pause in typing, the page builds a RegExp from the pattern and the ticked flags. If the constructor throws, its message is shown as is, because it names the problem better than a paraphrase would: "Unterminated group", "Nothing to repeat", "Invalid named capture referenced".

With g on, every match is collected with String.matchAll; without it, only the first. The count covers all matches, but only the first 1,000 are kept for the table and highlighting so the page stays usable on a sample full of short matches. The number of capture groups and their names are read by running the pattern with an extra empty alternative against an empty string, which works for any valid pattern.

The highlighted view splits the sample into plain and matched pieces and renders each matched piece as a mark element. Nothing you type is inserted as HTML, so a sample containing tags or script shows as text. An empty match, such as one from a* or a lookahead, appears as a thin bar at its position.

The explanation comes from a small tokenizer written for this page, separate from the browser's engine. It walks the pattern left to right and recognises literal text, escapes such as \d \w \s \b, character classes with ranges and negation, capturing, non-capturing and named groups, lookahead and lookbehind, alternation, the quantifiers * + ? {n} {n,} {n,m} and their lazy forms, the anchors ^ and $, and backreferences such as \1 and \k<name>. Anything else is marked as not explained.

The pattern, sample and replacement stay in the page's memory and are not put in the address bar. This tool writes nothing to localStorage, sessionStorage or IndexedDB, and the text is matched in this tab.

When to run it

  • Writing a validation pattern for order numbers, postcodes or SKUs and checking it against good and bad samples
  • Pulling timestamps, request IDs or error codes out of a chunk of server log
  • Preparing a find-and-replace for a code editor and previewing the result first
  • Understanding an old pattern inherited in a codebase by reading it piece by piece
  • Checking whether a pattern needs the m flag before using it on multi-line input

Running a check

  1. Type or paste the pattern into the Pattern box, without the surrounding slashes.
  2. Tick the flags you would pass in code; g is on by default so every match is shown.
  3. Paste a representative sample, including lines that should not match, into the Sample text box.
  4. Read the highlighted text and the match table, and adjust the pattern until the right pieces light up.
  5. Type a replacement such as $2/$1 to preview the rewritten text, then copy the pattern into your code.

Example

Pattern (\d{4})-(\d{2}) with the g flag, replacement $2/$1

2026-11 and 2027-01

Matches and replace preview

2 matches
#1  index 0   2026-11   group 1: 2026   group 2: 11
#2  index 12  2027-01   group 1: 2027   group 2: 01

After replacing: 11/2026 and 01/2027

Tips for best results

  • Include near-misses in the sample, such as 99-12 next to 2026-11, so you see what the pattern wrongly accepts.
  • Use named groups like (?<year>\d{4}); the table gets readable column headings and the replacement can say $<year>.
  • Add ^ and $ (with m for line-by-line input) when validating, otherwise a pattern matches anywhere inside a longer string.
  • Prefer [^,]* over .* when you mean "up to the next comma"; the greedy dot runs to the end of the line and backs up.
  • Turn g off to see exactly which match a non-global exec or match call would return in your code.

Limits

Some patterns take a very long time on some inputs. Nested repeats such as (a+)+ or (\w+\s?)*, run against a long string that almost matches, make the engine try an enormous number of paths. The page cannot stop a match once it starts, so the tab may freeze until the browser offers to end it. Reload, shorten the sample text, and rewrite the pattern so that no two repeats can match the same characters.

Backslashes in source code are doubled in string literals. The pattern \d+ typed here is written "\\d+" inside new RegExp("...") in code, but /\d+/ as a literal.

Without the u flag, an emoji or other character outside the Basic Multilingual Plane counts as two characters, so . matches only half of it.

The explanation describes syntax, not intent. It can tell you that [A-z] spans A to z; it will not warn that the range also includes [ \ ] ^ _ and the backtick.

Lookbehind and named groups need a reasonably recent browser. If a colleague's older browser rejects the pattern, the error appears for them, not for you.

Common errors and how to fix them

"Invalid regular expression: ... Nothing to repeat"
A quantifier such as * or + follows nothing it can repeat, often at the start of the pattern or right after (. Escape it as \* or \+ if you meant the character itself.
"Invalid regular expression: ... Unterminated group"
An opening ( has no closing ). Count the brackets, and escape a literal parenthesis as \(.
"Invalid regular expression: ... Invalid escape" with the u flag on
Unicode mode rejects escapes that legacy mode tolerates, such as \- outside a class or \a. Remove the backslash or turn u off.
The sample is over 100 KB and nothing is matched.
Paste only the lines that matter: a handful that should match and a handful that should not. Mistakes in a pattern usually show up on those lines as clearly as on a whole log file.

Best practices

  • Keep one sample per pattern that holds both lines that must match and lines that must not.
  • Anchor validation patterns at both ends.
  • Avoid repeats nested inside repeats; when you need them, make the inner and outer parts unable to match the same text.
  • Copy the pattern into a code comment with an example of what it matches, so the next reader does not have to decode it.

What the explainer covers and what it skips

The tokenizer handles the constructs that make up most everyday patterns. Its goal is to be right about what it describes, so it stops at the edge of what it knows.

  • Explained: literal text, . ^ $, \d \D \w \W \s \S \b \B, \n \r \t and similar control escapes, \xHH and \uHHHH
  • Explained: character classes such as [a-z0-9_] and negated classes such as [^,]
  • Explained: ( ), (?: ), (?<name> ), (?= ), (?! ), (?<= ) and (?<! )
  • Explained: * + ? {n} {n,} {n,m}, each with its lazy form, and the | alternation bar
  • Explained: backreferences \1 to \99 and \k<name>, and \p{...} when the u flag is on
  • Not explained: inline modifier groups such as (?i: ), the v flag's set notation, and any other syntax outside the list above

Greedy and lazy, in one example

Against the text <b>one</b><b>two</b>, the pattern <b>.*</b> matches the whole string, because .* is greedy: it grabs as much as it can and gives back only enough for </b> to match at the very end. The lazy version <b>.*?</b> stops at the first </b>, giving two matches. The match table shows the difference immediately, which is often quicker than reasoning it out.

Frequently asked questions

Is my sample text uploaded?

No. Matching runs in your browser's JavaScript engine and the text stays in the page's memory. There is no request carrying it and it is not saved to storage.

Will a pattern that works here work in Python or PHP?

Often, but not always. This page uses JavaScript's flavour. Simple patterns travel well; lookbehind, named group syntax and some escapes differ between languages, so test there too.

Why does the page say not explained for part of my pattern?

That piece is valid JavaScript but outside the subset the explainer was written for. The match results still include it, because the browser handles the matching.

Can the page stop a pattern that hangs?

No. A JavaScript regular expression runs to completion once started. If the tab freezes, close or reload it and try a shorter sample.