GenerateRandomSearch

Regular Expression Tester

A list of matches makes a simple claim — these spans, in this order, cover exactly these pieces of the text — and an off-by-one in a highlight offset produces a list that looks right and points at the wrong characters. Here the offsets are checked against the text itself: each match is sliced out by its own numbers, and the pieces between the matches must reassemble your input exactly.

What this generator does

Runs your pattern over the text, collects every match with its position and capture groups, and shows the text with the matches highlighted in place.

How to use this tool

  1. Type the pattern without the surrounding slashes, and any flags.
  2. Paste the text to search.
  3. Read the highlighted text, and the table of positions and groups.
  4. Tick the box to see what a replacement would give.

Understanding the controls

Pattern
Written without the surrounding slashes. Up to three hundred characters.
Flags
Any of g for every match, i for ignoring case, m for multiline, s for dot matching newlines, u for unicode and y for sticky.
Text to search
Up to four thousand characters. The limit is deliberate: a pattern that backtracks badly gets slower with the length of the text.
Replacement
Optional. Dollar-ampersand stands for the whole match and dollar-one for the first group, as in most languages.

Common use cases

  • Checking a pattern before putting it into code
  • Working out why a pattern matches more or less than expected
  • Seeing which capture group holds what
  • Previewing a find-and-replace before running it
  • Learning what a pattern somebody else wrote actually does

How this generator works

Three things are checked. Each match, sliced out of the text by its own start and end, must equal the text it claims to be. No two matches may overlap. And the pieces between the matches, interleaved with the matches themselves, must rebuild your input exactly — which cannot pass unless every offset is right. A pattern with a repeat inside a repeated group, such as one written (a+)+, can take exponential time on quite short text, so those are refused before anything runs rather than left to freeze the page.

Randomness and fairness

Nothing is random. The same pattern and text always give the same matches.

For how randomness is produced across the whole site, see how Generate Random works.

Limitations and good to know

  • The refusal of nested repeats is deliberately cautious and will turn away some patterns that would have been perfectly safe.
  • Four thousand characters of text, and three hundred of pattern.
  • A thousand matches at most; beyond that the list stops.
  • This is the flavour of regular expression the browser speaks, which differs in places from the ones in other languages.

Privacy and your data

The pattern and text stay in your browser. Nothing you type is transmitted or stored.