Regex Tester Calculator
Enter a pattern, optional flags and text to see which parts of the text match.
How the engine scans your text
A regular expression describes a set of strings, and the engine walks your text left to right, at each position trying to match the pattern starting from that spot. When a match succeeds it is recorded, and with the g flag scanning resumes just after the match, which is why matches never overlap. The result is an ordered list of every matched substring plus a total count.
This tester runs the JavaScript RegExp engine itself, so the results match exactly what new RegExp(pattern, flags) produces in any browser or Node script, including the error messages for invalid patterns.
Worked example at the defaults
The default pattern \d+ with the g flag runs against the text "Order #123 and #456 shipped on 7/1." and finds 4 matches: 123, 456, 7 and 1. The pattern means one-or-more consecutive digits, so it swallows all three digits of 123 in one match, restarts after the #, finds 456, then catches the two single digits in the date 7/1 as separate matches because the slash between them is not a digit.
Drop the g flag and the same pattern returns just 1 match, 123, because without g the engine stops after the first success. That single change from 4 matches to 1 is the most common reason a pattern seems to be broken when it is not.
The metacharacters worth memorizing
| Token | Meaning | Example match |
|---|---|---|
| \d | Any digit 0–9 | \d+ matches 456 in #456 |
| \w | Letter, digit or underscore | \w+ matches Order |
| \s | Space, tab or newline | a\sb matches a space after a |
| . | Any character except newline | 7/. matches 7/ in the date |
| + * ? | One-or-more, zero-or-more, optional | \d? matches one digit or nothing |
| ^ $ | Start and end of text | ^Order matches at position 0 |
| [0-9] | Custom character class | [0-9]{2} matches 45 in #456 |
| (ab|cd) | Group with alternation | (and|or) matches and |
Quantifiers such as + are greedy by default: they consume as much as possible before backtracking. .* swallows an entire line, which is why a lazy variant .*? — adding the question mark — is the usual fix when a pattern jumps from the first delimiter to the last instead of stopping at the nearest one.
What each flag changes
- g — find every match instead of stopping at the first
- i — case-insensitive, so ORDER matches order and Order alike
- m — makes ^ and $ match at the start and end of each line, not just the whole text
- s — lets . match newlines too, needed for single-line patterns spanning paragraphs
- u — full Unicode mode, so \d and character classes behave correctly with emoji and accents
Flags combine freely: gi finds every case-insensitive match. The default here uses g alone because digit matching has no case to ignore.
Common mistakes this tester exposes
- Forgetting the g flag, which silently caps the result at 1 match — the difference between 4 and 1 in the default example
- Greedy quantifiers like <.*> spanning from the first tag to the last instead of matching one tag
- Unescaped dots and slashes: a literal period must be written as \. or it matches any character
- Catastrophic backtracking from nested quantifiers such as (a+)+, which can hang the engine on adversarial input
- Anchoring habits: ^Order matches only the very start of the text unless the m flag is set
Reading an error message
An invalid pattern — an unbalanced bracket, a dangling backslash, an invalid group — surfaces the engine’s own error text, such as Unterminated character class or Invalid regular expression. The message names the syntax problem, not the location, but these errors are almost always a missing closing bracket or a backslash at the very end of the pattern.
From testing to shipping
Anchoring for validation
Once a pattern returns the expected matches here, paste it straight into code as new RegExp or a literal /pattern/flags. Two cautions travel with it: patterns applied to user input should be anchored when they are meant to validate a whole value — ^\d+$ rather than \d+ — and capture groups change what exec returns, so re-test with your real capture syntax before shipping. When the goal is counting words rather than finding patterns, the Word Frequency Counter does that job without any regex at all.
Frequently asked questions
How many matches does the default pattern find?
Four. The pattern \d+ with the g flag matches 123, 456, 7 and 1 in the text "Order #123 and #456 shipped on 7/1." — two order numbers and the two digits of the date, split by the slash.
Why do I only see one match?
The g flag is missing. Without it the engine stops after the first success, so \d+ on the default text returns only 123 instead of all 4 matches. Add g to the flags field to scan the whole text.
What does the g flag do exactly?
It lets the engine resume scanning after each match, collecting every non-overlapping match in order. Without g, exactly one match or none is returned, regardless of how many exist in the text.
Why does .* match too much text?
Quantifiers are greedy: .* consumes as much as possible. Switch to .*? — the lazy variant — to stop at the nearest possible end. For tag-like input, <.*?> matches one tag while <.*> spans from the first to the last.
What does \d+ mean?
\d matches any digit from 0 to 9, and + means one or more of the preceding item. Together they match any run of consecutive digits, so 123 matches as one piece and 7/1 matches as two pieces of one digit each.
How do I match a literal dot or slash?
Escape it with a backslash: \. for a period and \/ for a slash. An unescaped dot matches any character except a newline, which is why the pattern 7/. still works on the date 7/1 but also matches 7x.
Why is my pattern reported as an error?
The syntax itself is invalid — usually an unbalanced bracket, an unmatched parenthesis or a backslash at the end of the pattern. The tester shows the engine’s exact error message, and the fix is almost always a missing closing bracket.
Can regex matches overlap?
No. After each match, scanning resumes after the matched text. In 111 the pattern 11 finds one match at position 0, not two, because the second candidate overlaps the first.
How do I match something at the start of each line?
Use ^ with the m flag. Without m, ^ matches only at the very start of the whole text; with m it matches after every newline as well, so ^Order finds Order at the start of any line.
Method and sources
- ECMAScript regular expression syntax as implemented by the JavaScript RegExp engine.