Regex Tester

Type a regular expression, choose the flags and paste some test text. The tool marks every match in the text, lists each match with its position and capture groups, and can show what a replacement would produce. It uses the JavaScript regular expression engine in your browser, so what works here works in your JavaScript code and in HTML form validation.

What you enter

  • Pattern
  • g find all matches
  • i ignore case
  • m ^ and $ match at each line
  • s dot matches line breaks
  • u Unicode mode
  • Test text
  • Replacement (optional)
  • Show

What you get

  • Result
  • Number of matches

How it works

  1. Type the pattern without the surrounding slashes, for example \d+ or ^[a-z]+$.
  2. Tick the flags you want: g finds every match, i ignores case, m lets ^ and $ match at each line, s lets the dot match line breaks and u turns on full Unicode handling.
  3. Paste the text to search. Matches are wrapped in the marker characters in the result, and the table lists each one with its start position and groups.
  4. To preview a replacement, type the replacement text, switch the view to Replacement result and read the new text. Use $1, $2 or $<name> to insert groups and $& for the whole match.

Reading a regular expression

A regular expression is a small program for describing text. Ordinary characters match themselves. A character class such as [a-z] matches one character from a set, shorthand classes such as \d (a digit), \w (a letter, digit or underscore) and \s (a space) match common sets, and the dot matches any character except a line break. Quantifiers say how many: + means one or more, * means zero or more, ? means optional and {2,4} means between two and four.

Groups and alternation structure the pattern. (cat|dog) matches either word, and (?:cat|dog) does the same without capturing. The anchors ^ and $ pin the pattern to the start and end, and \b matches the edge of a word. Reading left to right and saying each piece in words is the quickest way to understand a pattern someone else wrote.

Testing patterns properly

A good test set has three kinds of text: typical input that should match, edge cases such as empty strings, very long strings and unusual characters, and near misses that should not match. For an email pattern, the near misses are things like a missing at sign, two at signs and a space in the middle. If your pattern accepts them, tighten it.

Remember that a regular expression can only judge the shape of text. A pattern can confirm that something looks like an email address or a date, but it cannot tell whether the mailbox exists or whether 31 February is a real day. For those jobs, validate the shape first and then check the meaning in code or by sending a confirmation message.

Tips

  • Without the g flag, only the first match is found. That is the usual reason a test finds one result where you expected many.
  • Anchors ^ and $ match the start and end of the whole text unless you tick m, in which case they match at every line.
  • Be careful with greedy patterns. .* grabs as much as it can, so <.*> on two tags in a row matches both as one. Use the lazy form <.*?> for the shortest match.
  • Some patterns can take exponentially long on certain text, for example nested repeats such as (a+)+. If the page freezes, simplify the pattern.
  • Test your pattern against text it should reject as well as text it should accept. A pattern that matches everything passes the first test and fails the second.

Frequently asked questions

Which regular expression flavour does this use?

It uses the JavaScript flavour (ECMAScript) built into your browser. Most everyday syntax is shared with other languages, but features like lookbehind, named groups and Unicode property escapes differ slightly, so test in the language you will really use.

What do the flags do?

g (global) returns every match instead of the first. i ignores upper and lower case. m (multiline) makes ^ and $ work on each line. s (dotAll) lets the dot match line breaks. u (unicode) treats the text as Unicode code points and enables property escapes like \p{L}.

What are capture groups?

Parentheses in a pattern capture the part of the match inside them. In (\w+)@(\w+)\.com applied to [email protected], group 1 is joe and group 2 is site. A named group such as (?<user>\w+) is also listed by name.

How does the replacement preview work?

It runs the JavaScript replace method with your pattern and replacement text. With the g flag every match is replaced, without it only the first. In the replacement, $1 is the first group, $2 the second, $& the whole match and $$ a literal dollar sign.

Why does the tester say my pattern is invalid?

The browser could not compile it. Common causes are an unclosed bracket or parenthesis, a quantifier with nothing before it, or a backslash at the very end. The message from the engine is shown so you can find the problem.

Related: Regex generator, JSON formatter and validator, Case converter.

Start for free or see all free tools. We.Inc is free to start, with no card needed. AI building is on paid plans from $20 a month.

More in Free Tools

Product

Who It's For

Features

Resources

Company

View Sitemap