Vanilla JavaScript Projects

Project 48, Developer tools

Regex Tester

Type a pattern and watch matches light up in your text. Groups, replacements and a plain-English explanation update as you type.

/\d+/
Engine
JavaScript RegExp
Flags
g i m s u y d v
Dependencies
None
Your browser
Checking

Pattern

//g

    

Matches

What your pattern means

    Common patterns

    Cheat sheet

    How it works

    1. Build the RegExpThe pattern and flags make a new RegExp. A syntax error is shown instead of crashing.
    2. Find matches safelymatchAll runs inside a Worker. If a pattern takes more than a second, the worker is stopped so the page never hangs.
    3. Show resultsMatch positions become highlighted spans. Groups fill the table and the replace preview runs on the same text.
    const re = new RegExp(pattern, "gd");            // d = give match indices
    for (const m of text.matchAll(re)) {
      console.log(m[0], m.index, m.groups);           // full match, where, named groups
      console.log(m.indices[1]);                      // start and end of group 1
    }
    // Stop runaway patterns like /(a+)+$/ from freezing the page
    const w = new Worker(url); w.postMessage({ pattern, flags, text });
    const t = setTimeout(() => { w.terminate(); showError("Too slow, stopped"); }, 1000);