Regex Tester

Test JavaScript regular expressions with live highlighting, capture groups, named groups and a replace preview. Flags, cheat sheet and common patterns.

Type a regular expression and some text, and see every match highlighted as you type. The table lists each match with its position and all capture groups — including named groups — and the replace box shows exactly what String.replace would produce with $1, $2 or $& (and named groups).

The tester uses your browser's JavaScript regex engine, so results match what your code will do. Each run is time-limited: a pattern that would freeze other testers is stopped after one second with an explanation.

How to use Regex Tester

  1. Type your regular expression between the slashes (without the surrounding / /).
  2. Switch flags on or off: g (all matches), i (ignore case), m (multiline), s (dot matches newlines), u (Unicode), y (sticky).
  3. Paste or type the text to test. Matches are highlighted and listed with their groups below.
  4. Optional: type a replacement such as $2, $1 to preview the replaced text and copy it.

Features

  • Live highlighting of every match, with alternating colours so neighbours are easy to tell apart.
  • Match table with positions, numbered groups and named groups.
  • Replace preview with $1, named groups, $& and $$.
  • All JavaScript flags: g, i, m, s, u and y.
  • Protection against catastrophic backtracking: runs stop after 1 second instead of freezing the tab.
  • Clear error messages for invalid patterns.
  • Common patterns ready to insert: email, URL, phone, date, IPv4, hex colour and more.
  • Built-in cheat sheet; nothing you type leaves the browser.

Regex cheat sheet

The most used building blocks of regular expressions in JavaScript.

PatternMatches
.Any character except a line break
\d \w \sA digit, a word character (A–Z, a–z, 0–9, _), whitespace
[abc] [^abc] [a-z]One of a, b, c · anything else · a range
^ $Start and end of the text (of each line with the m flag)
\bA word boundary
* + ?0 or more · 1 or more · 0 or 1 times
{3} {2,5}Exactly 3 · between 2 and 5 times
(abc) (?:abc)Capture group · non-capturing group (named groups too)
a|ba or b
(?=x) (?!x)Lookahead · negative lookahead (lookbehind is also supported)

Regex flags explained

Flags change how the whole pattern behaves. In JavaScript they are written after the closing slash, for example /cat/gi.

FlagNameEffect
gglobalFind all matches instead of stopping at the first
iignore caseA matches a
mmultiline^ and $ match at the start and end of every line
sdotAll. also matches line breaks
uunicodeFull Unicode: emoji count as one character, \p{L} works
ystickyMatch only exactly at the current position

Greedy vs lazy quantifiers

Quantifiers like * and + are greedy: they take as much text as possible and then give some back if needed. Adding ? makes them lazy, taking as little as possible. On the text "one" and "two", the pattern ".+" matches everything from the first quote to the last one, while ".+?" matches "one" and "two" separately. When a match is unexpectedly long, a lazy quantifier or a more specific character class such as [^"]+ is usually the fix.

What is catastrophic backtracking?

Some patterns can try an exponential number of ways to match before giving up — for example (a+)+$ on a long line of a's followed by a different character. In many tools this freezes the browser tab, and on a server it can be used for a denial-of-service attack (ReDoS). This tester runs each search in the background with a one-second limit and tells you when that happens. The fix is to remove nested quantifiers or make the inner part unambiguous.

Frequently asked questions

Which regex flavour does this tester use?

JavaScript (ECMAScript), using your browser's own engine — the same one your web code runs on. Most syntax is shared with Python, Java, PHP (PCRE) and .NET, but some features differ, such as possessive quantifiers and \A, which JavaScript does not support.

Why does my regex only find one match?

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

How do I use capture groups in the replacement?

Write $1, $2… for numbered groups, a $ followed by the group name in angle brackets for named groups, $& for the whole match and $$ for a literal dollar sign. For example, pattern (\w+)\s(\w+) with replacement $2 $1 swaps two words.

Why is my regex marked invalid?

There is a syntax error, such as an unclosed bracket or parenthesis, a quantifier with nothing before it (like a leading *), or a lookbehind or named group in a browser that is too old. The message from the regex engine is shown under the pattern.

How do I match a dot, a bracket or another special character?

Put a backslash in front of it: \. \( \[ \* \+ \? \\ . Inside square brackets most characters lose their special meaning, so [.] also matches a literal dot.

Is my text uploaded?

No. Matching runs in your browser in a background worker; nothing is sent to a server.

Related tools