Regex tester
Type a regular expression and some text to see every match highlighted, with its position and capture groups. It uses your browser's JavaScript engine, runs instantly and never uploads your text.
How to use it
- Type the pattern in Regular expression, without the slashes.
- Set the Flags, for example g for all matches or i to ignore case.
- Paste your sample in Test text.
- Check the highlighted text and the Matches table for positions and groups.
Complete guide to testing regular expressions
What a regex tester is for
A regular expression (regex) is a pattern that describes text: "four digits, a dash, two digits" or "a word, an at sign, a domain". Programmers use them to validate forms, search logs, extract values and rewrite strings. The syntax is dense, and a pattern that looks right often matches too much or too little. A tester lets you try the pattern against real sample text and see at once what it finds, where it finds it, and what each group captured, before you paste it into code.
How this tester works
The pattern runs with the JavaScript regular expression engine of your browser, so it behaves like new RegExp(pattern, flags) in a web page or in Node.js. Type only the pattern, with no surrounding slashes. Every match is highlighted in the text, and the table lists its number, the matched text, its position and its groups. Positions start at 0 and are shown as start-end, where the end is the index just after the last matched character. Capture groups appear as 1: value, and named groups as name: value. A group that did not take part in the match shows a dash. Nothing is uploaded, and an invalid pattern produces an error message instead of a result.
The flags
| Flag | Effect |
|---|---|
g | Global: find every match, not only the first |
i | Ignore case |
m | Multiline: ^ and $ match at each line, not only the whole text |
s | The dot also matches line breaks |
u | Unicode mode, for characters beyond the basic range |
y | Sticky: each match must start exactly where the previous one ended |
Worked examples
- With the default text
Contact: ana@example.com, luis@mail.com and bob@test.org, enter the pattern(?<user>\w+)@(?<host>\w+)\.comand flagg. The summary shows 2 matches. Match 1 isana@example.comat 9-24 with groups1: ana | 2: example | user: ana | host: example. Match 2 isluis@mail.comat 26-39. The address on.orgis not matched, because the pattern asks for.com. - Use the pattern
\d+onOrder 66 and 7. With the flags field empty you get one match,66at 6-8. Addgand you get two:66at 6-8 and7at 13-14. - Use
^\w+on two lines,one twoandthree four. Withgalone onlyone(0-3) matches, because^means the start of the whole text. Withgmyou also getthreeat 8-13.
Quick syntax reference
| Piece | Meaning |
|---|---|
\d \w \s | A digit, a word character (letter, digit or underscore), a whitespace |
. | Any character except a line break |
* + ? | Zero or more, one or more, zero or one |
{3} {2,5} | Exactly 3, between 2 and 5 |
[abc] [^abc] | One of these characters, any character except these |
( ) (?: ) | Capturing group, non-capturing group |
a|b | a or b |
\b | Word boundary |
Tips and common mistakes
- Seeing only one match almost always means the
gflag is missing. - Special characters such as
. ( ) ? + *must be escaped with a backslash when you want them literally. Escaping by hand is easy to get wrong; the string escape tool has a regular expression mode that does it for you. - Greedy quantifiers take as much as they can. Use
.*?for the shortest match. - The tester shows at most 1000 matches and then says so. Empty matches are labeled "(empty match)".
- A pattern with nested repetition, such as
(a+)+b, can freeze the page on some texts. Simplify it or shorten the sample. - Test with text that should not match as well as text that should.
Related tools
Pull emails or numbers out of a document with the extract from text tool, apply a replacement with find and replace, or compare results with the text diff.
Questions
Which regex flavor does it use?
The JavaScript (ECMAScript) flavor of your browser. Most syntax matches other languages, but features like lookbehind, named groups or possessive quantifiers can differ elsewhere.
Why do I only see one match?
Without the g flag a regular expression stops at the first match. Add g to the Flags field to find all of them.
How are positions counted?
Positions start at 0 and are shown as start-end, where end is the index right after the last character of the match.
Can a pattern freeze the page?
A badly written pattern with nested repetition can take a very long time on some texts (catastrophic backtracking). If the page stops responding, simplify the pattern or shorten the text.