Regex Tester
Test a regular expression as you type. The pattern is colour-coded and explained piece by piece, every match and capture group is highlighted in the text in matching colours and listed in a table, a replacement is previewed, and example strings that match are generated.
About this tool What it's for, how to use it and an example
What it's for
Build and check a regular expression (regex): a pattern for finding text, used in code, search boxes, editors and command-line tools. You see what the pattern matches as you type, and what each part of it means in plain English, so you can tell why it matches what it does, or why it doesn’t.
For example, when a pattern for pulling dates or IDs out of a log file matches too much, paste a few sample lines to see exactly which text each match and group takes.
How to use it
Type a pattern (without the surrounding slashes) and some text to test it on, or choose one of the Examples. Everything updates as you type.
- The pattern is colour-coded: groups in their own colours, quantifiers (
*,+,{2,4}) in orange, character classes ([a-z]) in purple, escapes such as\din green, anchors (^,$,\b) in cyan and|in red. - The text shows each match underlined, and each capture group coloured as it is in the pattern.
- Explanation breaks the pattern into parts, with groups and alternatives nested inside. Point at or focus a part to mark it in the pattern; select it to select that part in the pattern box. A pattern likely to be very slow is flagged.
- Matches lists each match and what each group captured. Select a position to select that match in the text.
- Replace shows the text with every match replaced. Use
$1or$<name>for a group,$&for the whole match, and$$for a dollar sign. - Generate examples makes the number of strings you ask for that match the pattern, and, under Near-misses, a few that look similar but don’t. Each is checked against your pattern with the chosen flags before it is shown; Copy copies a list.
- The flags are remembered in this browser. Copy as /pattern/flags copies the pattern as JavaScript code.
Example
Choose Repeated words from Examples, or type this pattern with the g and i flags:
\b(\w+)\s+\1\b
and this text:
This is is a test of the the tool.
The tool reports 2 matches, with 1 group: is is and the the, each with the repeated word as group 1. The
explanation reads \1 as “The same text that group 1 matched”. The replacement $1 gives
This is a test of the tool.
Good to know
Patterns are run by your browser’s own JavaScript regex engine, so the results are exactly what JavaScript code would get. Other languages differ in places: Python, PCRE (PHP) and .NET have features JavaScript lacks, such as possessive quantifiers and inline comments. The v flag isn’t offered. Matching is stopped after 2 seconds, which catches patterns that would otherwise freeze the page (catastrophic backtracking). The table lists the first 500 matches, and at most 2,000 are found and highlighted. Generated examples come from reading the pattern, so a repeat with no upper limit gets at most 5 more than its minimum, and lookarounds, anchors and word boundaries are only checked after the string is made: a pattern that depends on them may give fewer examples than asked for. A pattern with few possible strings gives fewer, as each example is different. Examples are random, and differ each time.
Guide: Reading a crash or a log
Explanation
Matches
Generate examples
Replace
Built with The third-party code and data this tool uses, and their licences
This tool uses no third-party code or data of its own: its code was written for this site.
Every page also uses Montserrat (SIL Open Font License 1.1).
Everything the site uses is on Credits and licences.
Private: this tool runs in your browser. Nothing you type, paste or choose leaves this page.
Saved you a few minutes? Say thanks with a coffee.
Something wrong with this tool, or missing from it? Report a bug or suggest a feature.