A regular expression rarely fails with an error message. It fails quietly: it finds nothing, one match fewer than you expected, or a match that runs a few characters too far. Work out which of these you have before you change the pattern.
Read the result before you touch the pattern
The Regex Tester gives you three readings. The Matches found counter says how many matches there are, the highlighted text shows where they are, and the table lists what each match and capture group contained.
The Matches and capture groups table has two position columns, Start and End. Start is zero-based, so the first character is at position 0. End is exclusive: it points one past the last matched character. In the Match report the same range is written as [4–7], which covers three characters: positions 4, 5 and 6. The examples below use that notation.
One match where you expected all of them
If only the first occurrence lights up, the cause is almost always a missing g flag. Without it, the engine stops after the first match, and a replacement changes only the first occurrence (MDN, String.prototype.replace). In the tester g is checked by default, so either the box was unchecked or your own code calls match() or replace() without g.
Take the pattern (\d+), the replacement [$1] and the text a12 b34:
with g: a[12] b[34]
without g: a[12] b34
Switch the Operation to Replace, and the Replacement preview shows this effect immediately. To swap one fixed piece of text for another, the find and replace tool is more direct.
The sticky flag y causes a related problem. Each match must start exactly where the previous one ended (lastIndex in the ECMAScript specification), and the engine does not scan forward. So \d with g and y on a1b2 finds nothing, because position 0 holds a letter. Without g, a sticky pattern returns at most one match.
Zero matches on text that clearly contains the value
The most common cause is anchors on multi-line text. Take the pattern ^\d+$ and this text:
12
34
Without m, this gives 0 matches; with m, it gives 2. Without the multiline flag, ^ and $ mean the start and end of the whole text, not of each line (MDN guide).
The second cause is pasted delimiters. The Regular expression field already sits between / delimiters, so /\d+/g typed into it searches for a literal slash, digits, another slash and the letter g. Type \d+ and set the flags with the checkboxes.
The third cause is double escaping. In new RegExp("\\d+"), the string literal turns \\ into one backslash before the engine sees it. Pasting \\d+ into the tester searches for a literal backslash followed by letters d. Paste the pattern as the engine receives it: \d+.
The fourth cause is invisible characters. A zero-width space or a byte order mark breaks a literal pattern even though the text looks identical, and a plain space will not match a non-breaking space. Finding zero-width spaces, BOMs and other hidden Unicode explains how to spot them.
Finally, check the case. The i flag is off by default, so Error does not match error.
A match that swallows too much
Here is the pattern ".*" against this text:
say "a" and "b"
You get one match, [4–15], from the first quote to the last, where you expected it to end at 7. The cause is that .* is greedy. It takes everything to the end of the text, then gives characters back only until a closing quote can match, and the last quote is the first one it reaches.
Both of these fixes give two matches, [4–7] and [12–15]:
".*?"uses a lazy quantifier, which takes as few characters as possible."[^"]*"uses a negated class, which takes any character except a quote.
The negated class is often better. It can never cross a closing quote, so its meaning does not depend on the rest of the pattern, and it leaves the engine fewer alternatives to try when the text does not match.
The whole match is right, but a group is empty
A capture group that did not take part in the match appears as — in the table and as unmatched in the Match report. That differs from a group that matched an empty string: an unmatched group never ran, while an empty group ran and captured zero characters.
Alternation is a common cause. With (\d+)|([a-z]+), each match fills only one of the two groups, so the other is always —. In a replacement, an unmatched group inserts an empty string for $1 or $2 (MDN, String.prototype.replace).
Group numbers follow the order of the opening parentheses. (?:...) does not capture and is not counted. A named group is still numbered, and the table shows it as Group N .
Positions that look shifted after an emoji
Offsets count UTF-16 code units, the unit JavaScript strings are made of (ECMA-262). Most emoji take two code units, so in 😀 ok the match ok has Start 3 and End 5, not 2 and 4.
These offsets agree with JavaScript string indices, so you can pass them straight to slice(). They may not agree with the column number your text editor shows, because editors count characters in different ways. With the u flag, zero-length matches do not split such a character in half.
When the page stops responding
JavaScript uses a backtracking engine, so some patterns force an exponential number of attempts on text that almost matches. Russ Cox measured Perl's backtracking engine taking over sixty seconds on a 29-character string that a Thompson NFA handled in about twenty microseconds (Russ Cox).
The tester refuses the most common trap: a group containing +, * or {n,} followed by another unbounded quantifier, such as (a+)+$. For that pattern it shows "This pattern contains nested unbounded quantifiers and may cause excessive backtracking. Simplify it before running."
The check is heuristic. It only inspects groups that contain no other group, so ((a+))+$ gets through, and so does overlapping alternation without a quantifier inside the group, such as (a|a)*b or (a|aa)+. There is no timeout, so these can freeze the tab on a long string. Test risky patterns on short text first.
To fix them, make alternatives mutually exclusive and replace nested quantifiers with a single one: (a+)+ matches the same text as a+. JavaScript has no possessive quantifiers or atomic groups, but Regular-Expressions.info describes a lookahead-plus-backreference workaround. For patterns that will run on other people's input, OWASP's ReDoS page lists the risky pattern families.
Limits that can look like a bug
- A run stops at 500 matches and shows a warning.
- The table shows at most 200 rows, and group rows count toward that total.
- Only the first 50 capture groups of each match are listed.
- The pattern can be up to 500 characters long, and the Test text up to 100,000.
- The Flags checkboxes offer only
g,i,m,s,uandy. There is nodorv, so set syntax that needsvwill not work. - Lookbehind and
\p{...}depend on your browser's engine, and\p{...}also needs theuflag (MDN guide).
If a result stops partway through a long log, check these limits before you change the pattern in the Regex Tester.
Privacy
Matching runs in your browser's built-in JavaScript regex engine, and your pattern and text are not uploaded. Only an anonymous usage record (mode, input length, run time and any error code) leaves the page.