spyc.ai

Free tool

Regex Tester

Test a JavaScript regular expression against sample text, with matches highlighted and capture groups listed.

How it works

What is the Regex Tester?

The Regex Tester is a free tool that runs a JavaScript regular expression against text you provide. Enter the pattern and flags, and it highlights every match in the text, lists each match with its position and capture groups, including named groups, and shows the engine's error message when the pattern is invalid.

How to use the Regex Tester

  1. Type the pattern without the surrounding slashes, or press Load sample.
  2. Set the flags, for example g for all matches or gi to also ignore case.
  3. Paste the text to test against.
  4. Check the highlighted matches and the group values listed under them.

What is a regex tester?

A regex tester lets you try a regular expression on sample text and see what it matches before you put it in code. Regular expressions are compact and easy to get subtly wrong, so watching one run on real examples is the quickest way to find the case it misses or the text it wrongly swallows.

This tester uses your browser's own JavaScript engine through new RegExp(pattern, flags). What matches here is what will match in JavaScript running in the same browser.

Which regex flavor does this tester use?

JavaScript, also called ECMAScript. Most everyday syntax is shared with PCRE, Python, Java and .NET: character classes, quantifiers, groups, alternation and anchors behave the same. The differences are at the edges. JavaScript has no possessive quantifiers or atomic groups, a bare inline flag such as (?i) at the start of a pattern is an error, and named groups are written (?<name>...).

So a pattern that works here will usually work elsewhere, but test it in the target language if it uses anything beyond the basics. Tools that take RE2 syntax, such as Google Analytics and Search Console filters, do not support lookaheads or backreferences at all.

What do the regex flags mean?

g (global) finds every match, where no flag stops at the first. i ignores letter case. m makes ^ and $ match at the start and end of each line, not only of the whole text. s lets the dot match line breaks. u turns on full Unicode handling, and y (sticky) matches only at the exact current position.

The most common surprise is forgetting g: the tester then reports one match, because that is what JavaScript does. Type the flags as letters with no spaces. An unknown or repeated flag is reported as an error.

How do capture groups work?

Parentheses capture the part of the match they enclose. Groups are numbered from 1 by the position of their opening parenthesis. (?<year>\d{4}) gives the group a name as well, and (?:...) groups without capturing.

Under the highlighted text, the tester lists each match with its start index and every group's value, with the name next to the number for named groups. A group that sits in a branch that was not used shows as did not take part, which is different from a group that matched an empty string.

Why does my regex match too much or freeze the page?

Matching too much is usually greed. .* takes as much as it can, so <.*> on a line with two tags matches from the first < to the last >. Use the lazy form .*? or a narrower class such as [^>]*.

Freezing is catastrophic backtracking. A pattern with nested quantifiers, such as (a+)+$, can take exponential time on text that almost matches, and because the pattern runs in your browser tab, the tab stalls. The tester cannot interrupt the engine. If the page hangs, reload it and simplify the pattern. The list stops at 1,000 matches. To rewrite matched text into another naming style, the Case Converter may save you a pattern, and the Text Diff Checker shows the effect of a replace run elsewhere.

Questions

Do I type the slashes around the pattern?
No. Enter only the pattern, and put the flags in their own field. /\d+/g in code is \d+ in the pattern field and g in the flags field.
Why is only one match found?
The g flag is missing. Without it a JavaScript regex returns the first match only.
Does it support lookbehind?
It supports whatever your browser's JavaScript engine supports. Current versions of Chrome, Edge, Firefox and Safari all handle lookbehind, written (?<=...) and (?<!...).
Can I test a regex for Python or PHP here?
For basic syntax, yes, because it behaves the same. Features that differ between flavors, such as inline flags, possessive quantifiers and some Unicode classes, need testing in that language.
Is the Regex Tester free?
Yes, with a free spyc.ai account. It runs in your browser, so nothing you type or paste is sent to a server.

Developer Tools

More in Developer Tools

JSON Formatter

Format, minify and validate JSON in your browser, with the line and column of the first syntax error.

Format JSON

JSON-LD Formatter

Pretty-print JSON-LD pasted bare or in script tags, list every @type and flag a missing @context.

Format JSON-LD

Case Converter

Convert text to upper, lower, title or sentence case, or to camelCase, PascalCase, snake_case and kebab-case.

Convert case

Ready when you are

Before your next buyer asks AI about you, ask AI about yourself

One check, four engines, about twenty seconds. We show you the answers before we ask for anything.