Regex Tester

Test JavaScript regular expressions with live highlighting, capture groups, and replace preview.

Flags
Examples (simplified):
Match highlighting
#IndexMatchGroups
Matches will appear here.
Regex cheat sheet (JavaScript)

Characters

. any char except line breaks (all with s)

\d \w \s digit, word char, whitespace

\D \W \S the opposites

[abc] [^abc] [a-z] sets and ranges

\p{L} Unicode letter (needs u)

Anchors

^ $ start / end of input (of line with m)

\b \B word boundary / not a boundary

(?=x) (?!x) lookahead

(?<=x) (?<!x) lookbehind

Quantifiers

* + ? 0+, 1+, 0 or 1

{3} {2,} {2,5} exact, at least, range

*? +? ?? lazy (as few as possible)

Groups

(x) capture as $1, $2…

(?<name>x) named capture

(?:x) group without capturing

\1 \k<name> back-references

a|b alternation

Runs entirely in your browser — your pattern and text are never uploaded.

Advertisement

How to Use This Regex Tester

Type a pattern into the field between the two slashes and paste the text you want to search into the Test string box. Matches are highlighted in alternating shades of yellow so that adjacent matches stay distinguishable, and the status line reports how many matches were found and how long the search took. The tester waits until you pause typing for a quarter of a second before it runs, so a half-written pattern does not flash errors at every keystroke. If you are working with a very large block of text, untick Update as I type and press Test Regex (or Ctrl/Cmd + Enter) when you are ready.

The flag toggles under the pattern map directly to JavaScript's flags, and the letters you enable appear after the closing slash, exactly as they would in a regex literal such as /\d+/gi. The Matches & Groups tab lists every match with its start and end index and the value of each capture group; named groups are labelled with their names. Switch to Replace to type a replacement string and see the transformed text immediately. The example buttons load a pattern, flags, sample text, and a replacement so you can see a working setup and then adapt it.

Which Regex Flavor Is This? JavaScript vs PCRE and Python

Regular expressions look similar across languages, but each engine has its own dialect. This page does not emulate anything: it hands your pattern to the browser's own RegExp implementation, so the results are what your JavaScript or TypeScript code will produce in a current browser or in Node.js. That is useful when you are writing front-end validation, a build script, or a string transformation in JavaScript, and it means you should be careful when copying patterns from elsewhere.

  • Missing in JavaScript: possessive quantifiers (a++), atomic groups ((?>...)), recursion, conditionals, \A and \Z anchors, and inline flag groups like (?i) at the start of a pattern. PCRE (used by PHP and many editors) supports these.
  • Named groups: JavaScript and PCRE use (?<name>...); Python's re module uses (?P<name>...). In replacements, JavaScript uses $<name> while Python uses \g<name>, and Python writes numbered references as \1 rather than $1.
  • Lookbehind: JavaScript allows variable-length lookbehind such as (?<=\$\d+). Python's re requires fixed width, and PCRE has historically required each alternative to have a bounded length.
  • Dot and line breaks: without the s flag, the dot in JavaScript does not match \n, \r, or the Unicode line and paragraph separators.
  • Unicode: without the u flag, JavaScript works on UTF-16 code units, so an emoji counts as two "characters" and \p{...} property escapes are unavailable.

What Each Flag Does

Advertisement

The g (global) flag tells the engine to keep searching after the first match. Without it, replace() changes only the first occurrence and exec() always starts from the beginning. The i flag makes matching case-insensitive. The m (multiline) flag changes the meaning of ^ and $ so that they match at the start and end of every line instead of only at the start and end of the whole input; it does not change what the dot matches. That is the job of s (dotAll), which lets . match line breaks too.

The u flag switches on full Unicode mode. Patterns then treat a surrogate pair as one character, you can write code point escapes such as \u{1F600} and property escapes such as \p{Lu}, and the syntax becomes stricter: escapes that mean nothing, like \- outside a character class, become errors instead of being silently ignored. The y (sticky) flag anchors each attempt at the current lastIndex. A sticky regex will not skip ahead to find a later match, which makes it handy for writing tokenizers that consume input piece by piece.

How the Tester Finds Matches Without Locking Up

With the global flag on, the tester calls exec() in a loop and records each result, the same technique that String.prototype.matchAll uses internally. One subtle problem with such a loop is the zero-length match. A pattern like ^ with the m flag, a word boundary \b, or a* applied to text with no letter a can succeed without consuming anything. If the search position never moves, a naive loop would find the same empty match forever. The tester detects an empty match and advances the position by one character, or by a whole code point when the u flag is set, so it never repeats itself. Zero-length matches are drawn as thin blue markers in the highlighted view so you can see exactly where they occur.

To keep the page responsive, at most 500 matches are rendered in the table and preview, counting stops after 100,000 matches or once the loop has run for roughly 400 milliseconds, and live updates pause automatically above 200,000 characters of input. These limits affect only what the page displays; the regular expression itself is unchanged.

Capture Groups, Named Groups, and Replacement Syntax

Parentheses do two jobs: they group part of a pattern so that a quantifier or alternation applies to the whole group, and they capture the matched text for later use. Captures are numbered from 1 by the position of their opening parenthesis, reading left to right, even when groups are nested. If you only need grouping, write (?:...) so the numbering of your other groups does not shift. Named groups, (?<year>\d{4}), make long patterns far easier to maintain because code can read match.groups.year instead of relying on a position that changes when someone edits the pattern.

A group that did not take part in the match, such as the unused side of an alternation, is reported as undefined rather than as an empty string; the two are different in JavaScript and the table shows which one you got. In replacement strings, $1 through $99 insert numbered groups, $<name> inserts a named group, $& inserts the entire match, $` and $' insert the text before and after it, and $$ produces a literal dollar sign. Inside the pattern itself, \1 or \k<name> refer back to what a group already matched, which is how the repeated-word example finds "the the".

Practical Uses for Regular Expressions

Regex shines when you need to recognise a shape of text rather than a fixed string. Common jobs include pulling IDs, dates, or URLs out of log files; normalising user input such as phone numbers before saving it; validating the format of a form field on the client; renaming files or symbols across a codebase with an editor's search-and-replace; and splitting text on flexible separators such as /\s*[,;]\s*/. Many command-line tools, linters, and routing libraries accept regular expressions in their configuration as well, so a tested pattern often travels well beyond a single script.

The examples on this page are deliberately simplified. The email pattern accepts the addresses people commonly type, but the full address grammar in RFC 5322 allows quoted local parts and other forms no short pattern handles; the only reliable test that an address works is sending a message to it. Likewise, the date pattern checks the month and day ranges but will happily accept February 31. For anything where correctness matters, use a regex to find candidates and then validate them with real parsing code, such as new URL() for links or a date library for calendar dates.

Common Pitfalls and Performance Tips

The classic trap is catastrophic backtracking. A pattern such as (a+)+$ or (\w*\s?)* nests one quantifier inside another, giving the engine an exponential number of ways to divide the input. On text that nearly matches, a backtracking engine like the one in browsers and Node.js may try them all, freezing the tab or, on a server, creating a denial-of-service risk known as ReDoS. A running match cannot be cancelled from page code, so the tester warns when it sees a quantified group containing a quantifier. The fix is usually to make alternatives mutually exclusive or to remove the nesting: a+$ describes the same strings as (a+)+$.

Other frequent mistakes include forgetting to escape metacharacters (a literal dot is \.), writing ^ and $ without the m flag when you meant each line, using greedy .* where a lazy .*? or a negated class like [^"]* was intended, and reusing a global regex object in code. A regex with g or y remembers its lastIndex between calls to test() and exec(), so calling test() twice on the same string can return true and then false. When you build a pattern from user input with new RegExp(), escape special characters first, or a stray parenthesis will either throw an error or change the meaning of the pattern.

Frequently Asked Questions

Which regex flavor does this tester use?

It uses your browser's built-in JavaScript engine, so patterns behave exactly as they would with new RegExp() or a regex literal in modern JavaScript (the ECMAScript standard). That means features such as possessive quantifiers, atomic groups, recursion, conditionals, and inline modifiers like (?i) from PCRE are not available, while lookbehind and named groups written as (?<name>...) are.

Why does my pattern only find one match?

Without the g (global) flag, JavaScript stops after the first match, which is what exec(), match(), and replace() do in your own code too. Turn on g to list and replace every match.

How do I reference capture groups in the replacement?

Use $1, $2 and so on for numbered groups, $<name> for named groups, $& for the whole match, $` and $' for the text before and after the match, and $$ for a literal dollar sign. These are the same tokens that String.prototype.replace understands.

What does a zero-length match mean?

Some patterns can succeed without consuming any characters, for example ^, \b, lookarounds, or a* where there is no a. The tester marks these positions with a thin cursor and steps forward one character (or one full code point with the u flag) after each one, so the search always finishes.

Can a regular expression freeze the page?

Yes. Patterns with nested quantifiers such as (a+)+$ can take exponential time on input that almost matches, which is called catastrophic backtracking. A single match attempt cannot be interrupted in the browser, so the tester warns about risky shapes, waits for you to stop typing before running, and lets you switch off live updates for very large inputs.

Is my test data sent anywhere?

No. The pattern, the test string, and the replacement are processed by JavaScript running in this page. Nothing is uploaded or stored on a server.

Related Articles

Expert guides and educational resources to help you master the math.

Advertisement