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

  1. Type the pattern in Regular expression, without the slashes.
  2. Set the Flags, for example g for all matches or i to ignore case.
  3. Paste your sample in Test text.
  4. 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

FlagEffect
gGlobal: find every match, not only the first
iIgnore case
mMultiline: ^ and $ match at each line, not only the whole text
sThe dot also matches line breaks
uUnicode mode, for characters beyond the basic range
ySticky: each match must start exactly where the previous one ended

Worked examples

  1. With the default text Contact: ana@example.com, luis@mail.com and bob@test.org, enter the pattern (?<user>\w+)@(?<host>\w+)\.com and flag g. The summary shows 2 matches. Match 1 is ana@example.com at 9-24 with groups 1: ana | 2: example | user: ana | host: example. Match 2 is luis@mail.com at 26-39. The address on .org is not matched, because the pattern asks for .com.
  2. Use the pattern \d+ on Order 66 and 7. With the flags field empty you get one match, 66 at 6-8. Add g and you get two: 66 at 6-8 and 7 at 13-14.
  3. Use ^\w+ on two lines, one two and three four. With g alone only one (0-3) matches, because ^ means the start of the whole text. With gm you also get three at 8-13.

Quick syntax reference

PieceMeaning
\d \w \sA 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|ba or b
\bWord boundary

Tips and common mistakes

  • Seeing only one match almost always means the g flag 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.