navaja.dev / regex

Regex tester

Write a regular expression and see every match highlighted as you type, with its capture groups. Try find and replace, or start from a common pattern. Uses your browser's JavaScript regex engine, and nothing is uploaded.

Matches
As code:

Match details

    Common patterns

    How to test a regular expression

    1. Type the pattern between the slashes, or paste a whole literal like /\d+/g and the flags are picked up for you.
    2. Paste your test text. Matches are highlighted as you type, and each one is listed with its position and capture groups.
    3. Turn on Replace to preview String.replace with references like $1 or $<year>, then copy the result or the regex as code.

    Regex cheat sheet

    Flags

    g finds every match instead of the first. i ignores case. m makes ^ and $ match at each line. s lets . match line breaks. u enables full Unicode (code points, \p{…}). v is the newer Unicode mode with set operations like [\p{L}--[a-z]]. y (sticky) matches only at lastIndex. d records the start and end of every group, shown in the match details.

    Catastrophic backtracking

    Patterns with nested quantifiers, like (a+)+$ or (.*,)*, can take exponential time on text that almost matches. That freezes browsers and can take down servers (ReDoS). This tester runs every regex in a background worker and stops it after 1.5 seconds, so a slow pattern shows a warning instead of hanging the page. Rewrite such patterns to remove the nesting or make the alternatives exclusive.

    Questions

    Which regex flavor does this use?

    JavaScript (ECMAScript), using your browser's own engine, so results match what new RegExp() and String.match() do in your code. Most syntax is shared with PCRE, Python and Java, but some features differ (for example possessive quantifiers and atomic groups don't exist in JavaScript).

    Why does my regex only find the first match?

    Turn on the g (global) flag. Without it, JavaScript stops at the first match.

    How do I match across several lines?

    Use the s flag so . matches line breaks, or [\s\S] to match any character. Use the m flag if you want ^ and $ to match at the start and end of each line.

    How do named groups work?

    Write (?<name>...) to capture into a named group. In the match details it appears as <name>, in replacements use $<name>, and in code read it from match.groups.name.

    How do I escape special characters?

    Put a backslash before any of . * + ? ^ $ { } ( ) | [ ] \ / to match it literally, for example \. for a dot. Inside new RegExp("...") strings, double the backslashes.

    Is my text uploaded anywhere?

    No. Patterns and text stay in your browser. The regex runs in a local Web Worker so it can be stopped if it hangs.

    Why was my pattern stopped?

    It ran longer than 1.5 seconds, which almost always means catastrophic backtracking from nested quantifiers like (a+)+ or (.*)*. The same pattern could freeze a user's browser or a server, so it's worth fixing.