RegEx Tester
Test a regular expression against a string. Uses the JavaScript RegExp engine, runs entirely in your browser.
How it works
This uses JavaScript's native RegExp engine, not PCRE or .NET
regex, whose syntax differs slightly. With the g flag all matches
are found, without it only the first. Named capture groups
((?<name>...)) are shown separately from positional groups
in the result.
Greedy versus lazy matching
This single concept explains most regex results that look wrong.
Quantifiers are greedy by default: they match as much as
possible, then give characters back only if the rest of the pattern fails.
Matching <.+> against
<div>text</div> does not match just
<div>. It matches the entire string, because
.+ grabs everything and then backtracks to the final
>.
Adding ? makes a quantifier lazy, matching as
little as possible. <.+?> correctly matches
<div> alone.
| Pattern | Against | Matches |
|---|---|---|
<.+> | <a>b</a> | <a>b</a> (all of it) |
<.+?> | <a>b</a> | <a> |
".*" | "a" and "b" | "a" and "b" |
".*?" | "a" and "b" | "a" |
A more precise alternative to lazy matching is a negated character class.
"[^"]*" matches a quoted string by explicitly excluding the
closing delimiter, which is both faster and clearer than ".*?".
Syntax reference
Character classes
| Syntax | Meaning |
|---|---|
. | Any character except newline (unless the s flag is set) |
\d / \D | Digit / non-digit |
\w / \W | Word character ([A-Za-z0-9_]) / non-word character |
\s / \S | Whitespace / non-whitespace |
[abc] | Any one of a, b, c |
[^abc] | Any character except a, b, c |
[a-z] | Any character in the range a to z |
Anchors
| Syntax | Meaning |
|---|---|
^ | Start of string (or line, with the m flag) |
$ | End of string (or line, with the m flag) |
\b / \B | Word boundary / non-word-boundary |
Quantifiers
| Syntax | Meaning |
|---|---|
* | 0 or more |
+ | 1 or more |
? | 0 or 1 |
{n} | Exactly n |
{n,} | n or more |
{n,m} | Between n and m |
*?, +?, ?? | Lazy (non-greedy) versions of the above |
Groups & alternation
| Syntax | Meaning |
|---|---|
(...) | Capturing group |
(?:...) | Non-capturing group |
(?<name>...) | Named capturing group (shown separately in results) |
a|b | Alternation, a or b |
Lookaround
| Syntax | Meaning |
|---|---|
(?=...) | Positive lookahead |
(?!...) | Negative lookahead |
(?<=...) | Positive lookbehind |
(?<!...) | Negative lookbehind |
Flags
| Flag | Meaning |
|---|---|
g | Global, find all matches, not just the first |
i | Case-insensitive |
m | Multiline, ^/$ match at line boundaries, not just string boundaries |
s | Dotall, . also matches newlines |
u | Unicode, correct handling of astral code points (e.g. emoji) and stricter syntax validation |
y | Sticky, matches only starting exactly at lastIndex, no scanning ahead |
d | Generates start/end indices for each capture group (not shown separately in this tool's output, but a valid flag) |
Examples
| Pattern | Flags | Test string | Matches |
|---|---|---|---|
\d+ | g | abc 123 def 456 | 123, 456 |
(\w+)@(\w+) | alice@example | alice@example (groups: alice, example) | |
(?<year>\d{4})-(?<month>\d{2}) | g | 2023-11 and 2024-01 | named groups: {year: "2023", month: "11"}, {year: "2024", month: "01"} |
\d+(?=px) | width: 10px | 10 (the "px" itself is not part of the match) |
Useful patterns
| Purpose | Pattern |
|---|---|
| Digits only | ^\d+$ |
| Hex colour | ^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$ |
| IPv4 address (rough) | ^(?:\d{1,3}\.){3}\d{1,3}$ |
| ISO date | ^\d{4}-\d{2}-\d{2}$ |
| Quoted string | "[^"]*" |
| Trailing whitespace | [ \t]+$ |
| Duplicate word | \b(\w+)\s+\1\b |
| HTML tag | <([a-z]+)[^>]*> |
Two cautions on these. The IPv4 pattern accepts values above 255, since validating the numeric range in regex is verbose, check the ranges separately in code. And parsing HTML with regex works only for narrow, controlled input, nested and malformed markup requires a real parser.
Common mistakes
Forgetting to escape special characters
A dot matches any character, so example.com also matches
exampleXcom. Escape it as example\.com. The
characters needing escapes are
. * + ? ^ $ { } ( ) | [ ] \ /.
Anchors missing on validation patterns
Validating with \d{4} succeeds on
abc1234xyz, because it only requires four digits
somewhere. Validation patterns need ^ and
$ to constrain the whole string.
Catastrophic backtracking
Nested quantifiers such as (a+)+b can take exponential time on
a non-matching input, since the engine tries an explosive number of ways to
split the input. On a public-facing service this is a denial-of-service risk.
Avoid nesting quantifiers and prefer explicit character classes.
Assuming lastIndex resets
A RegExp with the g flag keeps a
lastIndex between calls to test(), so repeated calls
on the same object return alternating results. Either create the regex fresh
each time or reset lastIndex to 0.
Frequently asked questions
What happens with an invalid pattern?
You get a clear error message instead of a silent failure, for example on unbalanced parentheses or an unknown flag.
Why is PCRE or .NET regex syntax not fully supported?
This tool uses the browser's built-in JavaScript RegExp engine so it can run entirely client-side. The syntax is very close to PCRE and .NET but not identical, notably possessive quantifiers and atomic groups do not exist in JavaScript.
What is the difference between a capturing and non-capturing group?
(...) captures the matched text for later reference,
(?:...) groups without capturing. Use the non-capturing form when
you only need grouping for alternation or quantifiers, it keeps group
numbering clean and is marginally faster.
How do I match across multiple lines?
Two different flags. m makes ^ and
$ match at line boundaries. s makes .
match newlines as well. They solve different problems and are often needed
together.
Why does my pattern match nothing when it looks correct?
Common causes: a missing g flag when expecting multiple
matches, an unescaped special character, or greedy matching consuming more
than intended. Test with a simplified pattern and add pieces back one at a
time.
Is my test string sent anywhere?
No. Matching runs entirely in your browser using the native RegExp engine.
Related tools
- RegEx builder, an interactive editor with live highlighting and explanations
- Case converter, for identifier renaming
- Text diff viewer, to compare before and after
- All developer tools