Last reviewed:
What is a regular expression?
A regular expression (regex) is a small pattern language for describing text. \d{4}-\d{2}-\d{2} means "four digits, a hyphen, two digits, a hyphen, two digits", which matches dates such as 2026-10-08. Patterns combine literal characters with character classes (\d, \w, [a-f]), quantifiers (*, +, ?, {2,5}), anchors (^, $, \b), alternation (a|b) and groups in parentheses. Groups can capture the text they match so you can reuse it, for example in a replacement.
Every language has its own regex flavour, and the differences matter. This tester uses the JavaScript engine built into your browser, the same one used by String.prototype.match, replace and Node.js. It supports named groups ((?<year>\d{4})), lookahead ((?=…), (?!…)) and lookbehind ((?<=…), (?<!…)). It does not support some features of PCRE, Python or Java, such as possessive quantifiers (a++), atomic groups, inline modifiers like (?i) and recursion. A pattern that works here can still fail elsewhere, and the reverse is also true.
Flags change how the whole pattern behaves. g finds every match instead of the first. i ignores case. m makes ^ and $ match at the start and end of each line instead of the whole string. s lets . match newlines. u turns on full Unicode mode with stricter syntax. y (sticky) only matches at the exact position where the previous match ended.
How to use this tool
- Type your pattern between the two slashes at the top. Do not add the slashes or flags yourself; the flags currently on are shown after the closing slash.
- Tick the flags you need. g and m are on by default. If the pattern is invalid, the error from the JavaScript engine appears under the pattern.
- Paste text into Test string. Matches are highlighted in place, in alternating colours, so adjacent matches are easy to tell apart.
- Read the Matches panel. Each match shows its number, its index (the character position where it starts) and the value of every capture group, labelled with its name if it has one. Groups that did not take part in the match are marked "not matched".
- Type a replacement in Replace to preview
String.replace. Use$1,$2for numbered groups,$<name>for named groups,$&for the whole match and$$for a literal dollar sign. - Use Copy matches to copy every match, one per line, or Copy result to copy the replaced text. Sample loads an email-matching example and Clear empties the test string.
Examples
Extract fields from log lines
Pattern, with the g and m flags:
^(?<date>\d{4}-\d{2}-\d{2}) (?<time>[\d:]{8}) (?<level>[A-Z]+)\s+\[(?<svc>\w+)\].*?\((?<ms>\d+) ms\)$
Test string:
2026-10-08 12:01:33 ERROR [payments] Card declined for order 8841 (1290 ms)
2026-10-08 12:01:35 INFO [orders] Order 8842 created (42 ms)
2026-10-08 12:01:40 WARN [payments] Retry 2 for order 8841 (2011 ms)
Three matches. The first has the groups date = 2026-10-08, time = 12:01:33, level = ERROR, svc = payments and ms = 1290. Without the m flag, ^ and $ would refer to the whole text and there would be no match at all.
Reorder a date with a replacement
Pattern: (\d{4})-(\d{2})-(\d{2})
Replacement: $3/$2/$1
Input: 2026-10-08, 1999-12-31
Result: 08/10/2026, 31/12/1999
With the g flag turned off, only the first date would be rewritten.
Greedy and lazy quantifiers
Text: <b>bold</b> and <i>it</i>
Pattern <.+> one match: <b>bold</b> and <i>it</i>
Pattern <.+?> four matches: <b> </b> <i> </i>
.+ takes as much as it can and then backs off only far enough to find a >. Adding ? makes it take as little as possible.
Common errors and how to fix them
- "Unterminated group" for a pattern like
(abc. A parenthesis is not closed. To match a literal bracket, escape it:\(. - "Unterminated character class" for
[a-. A[was opened without a]. Escape it as\[if you meant the character. - "numbers out of order in {} quantifier" for
a{2,1}. The minimum must come first:a{1,2}. - "Duplicate capture group name". Two groups share a name. Rename one, or use a non-capturing group
(?:…)if you do not need the value. - "\ at end of pattern". A backslash has nothing to escape. In code you may have lost a backslash when copying from a string literal:
"\\d"in JavaScript source is the pattern\d. Paste the pattern itself here, not the quoted string. - Matches too much. An unescaped
.matches any character, soexample.comalso matchesexampleXcom. Useexample\.com. Greedy.*can also run past the point you wanted; try.*?or a negated class such as[^"]*. - Only one match. The
gflag is off. The status line reminds you to add it. - Pattern matches inside longer text.
\d{5}finds12345insideabc123456. Add anchors (^\d{5}$) or word boundaries (\b\d{5}\b) when validating a whole value.
When developers use this
- Writing a validation pattern for a form field, such as a postcode or an order reference, and testing it against valid and invalid samples before it goes into code.
- Building a search-and-replace for an editor or a
sed-style script, such as renaming a function across many call sites or converting date formats. - Pulling fields out of log lines when setting up parsing rules for a logging pipeline, or answering a quick question from a log file.
- Debugging an existing pattern from a codebase that rejects input it should accept, by pasting the pattern and the failing value side by side.
- Checking routing or rewrite rules written in JavaScript, such as path patterns in a Node.js server or a bundler configuration.
Privacy and how it works
Matching runs entirely in your browser tab using the native JavaScript RegExp object and String.matchAll. The pattern and test string are not uploaded or saved. That makes it safe to test against real log lines, customer records or configuration files that may contain emails, tokens or internal hostnames.
Limitations
- Only the JavaScript flavour is tested. Check differences before copying a pattern into Python, Java, Go (RE2, which has no lookarounds or backreferences) or PCRE-based tools.
- The
d(indices) andv(unicode sets) flags are not offered. - To keep the page responsive, counting stops at 5,000 matches and the list shows the first 500.
- A badly written pattern with nested quantifiers, such as
(a+)+$, can take exponential time on some inputs (catastrophic backtracking) and freeze the tab. If the page stops responding, reload it and simplify the pattern.
FAQ
Which regex engine does this use?
Your browser's JavaScript engine (V8 in Chrome and Edge, SpiderMonkey in Firefox, JavaScriptCore in Safari). They implement the same ECMAScript standard, so results match Node.js and front-end code.
Why is a match shown with "empty match"?
Some patterns can match zero characters, such as ^ with the m flag or \d* on a letter. Each of those positions counts as a match with an empty value. Usually it means a * should be a +.
How do I match across lines?
Turn on the s flag so . also matches newline characters, or use [\s\S] in place of .. The m flag does something different: it changes where ^ and $ match.
How do I reference a named group in the replacement?
Use $<name>, for example $<user> at $<domain> as in the sample. Numbered references such as $1 still work for named groups, because they are numbered in order too.
Why does my pattern work here but not in grep or Python?
Each tool has its own syntax. Basic grep needs \( for groups and does not know \d; Python names groups with (?P<name>…); Go does not support lookbehind. Translate the pattern for the target engine and test there too.
How do I match a literal special character?
Escape it with a backslash: \., \*, \?, \(, \[, \{, \|, \^, \$ and \\. Inside a character class most characters are literal, so [.] also matches only a dot.
Should I use a regex to parse HTML, JSON or URLs?
For quick searches it is fine, but not for real parsing. Those formats allow nesting, escaping and quoting that patterns handle badly. Use DOMParser, JSON.parse or the URL class in code.
What does the index in each match mean?
It is the zero-based character position in the test string where the match starts, as returned by JavaScript. Line breaks count as one character each.