NinjaFunction (Ninja Function) logo

Regex Tester

Test regular expressions online with live match highlighting, capture groups, flags and replace. Free and private.


Your pattern

//g

Flags

Try an example

Runs in your browser with the JavaScript regex engine. Nothing is sent to a server.

Results

Results will appear here.

About This Tool

NinjaFunction's Regex Tester lets you write a regular expression and see the result straight away. Matches are highlighted in your text, every match is listed with its position and capture groups, and you can switch flags on and off, try a replacement, or start from a ready example such as an email, date or web address. The page also has a regex cheat sheet. Patterns run in your browser with the JavaScript engine, and a pattern that runs too long is stopped for you, so nothing is sent to a server and the page never freezes.

πŸ”‘ How to Use

  1. Type your pattern between the two slashes. It starts with an example so you can see how it works.
  2. Choose your flags. Turn on Global to find every match, and Ignore case if capital letters should not matter.
  3. Paste your text in the test box. Matches are highlighted right away on the right.
  4. Read the matches. Each match shows where it was found and what every group captured.
  5. Try a replace. Type a replacement such as $1 to see the text after the change.

πŸ’‘ Pro Tips

  • βœ” Click an example to fill in a ready pattern, text and flags.
  • βœ” Start simple. Build the pattern in small steps and check the highlight after each one.
  • βœ” Use named groups like (?<year>\d{4}) and the replacement $<year> to keep patterns readable.
  • βœ” Hover over a flag to see what it does.

Regex Cheat Sheet

These are the building blocks you will use most. Combine them to describe almost any piece of text.

Characters

PatternWhat it matches
.Any character except a line break
\dA digit (0 to 9)
\DAnything that is not a digit
\wA letter, digit or underscore
\WAnything that is not a letter, digit or underscore
\sA space, tab or line break
\SAnything that is not whitespace
[abc]One of the characters a, b or c
[^abc]Any character except a, b or c
[a-z]Any lowercase letter from a to z

Position

PatternWhat it matches
^The start of the text (or of each line with the m flag)
$The end of the text (or of each line with the m flag)
\bThe edge of a word
\BNot the edge of a word

How many

PatternWhat it matches
*Zero or more times
+One or more times
?Zero or one time (optional)
{3}Exactly 3 times
{2,5}Between 2 and 5 times
{2,}2 or more times
*?As few times as possible (lazy)

Groups

PatternWhat it matches
(abc)A capture group. Its text is kept as Group 1, 2, 3 and so on
(?:abc)A group that does not capture
(?<name>abc)A group with a name you choose
a|bEither a or b
\1The same text that Group 1 matched
(?=abc)Followed by abc (lookahead)
(?!abc)Not followed by abc
(?<=abc)Preceded by abc (lookbehind)
(?<!abc)Not preceded by abc

What the Flags Do

FlagNameEffect
gGlobalFinds all matches instead of stopping at the first
iIgnore caseTreats upper and lower case letters as the same
mMultilineMakes ^ and $ match at the start and end of every line
sDot matches newlineLets the dot match line breaks as well
uUnicodeHandles emoji and other special characters correctly

Ready-to-Use Patterns

FindsPattern
Email address[\w.+-]+@[\w-]+(?:\.[\w-]+)+
Web addresshttps?:\/\/[^\s/$.?#][^\s]*
Date (YYYY-MM-DD)\d{4}-\d{2}-\d{2}
IPv4 address\b(?:\d{1,3}\.){3}\d{1,3}\b
Hex colour#(?:[0-9a-fA-F]{3}){1,2}\b
Whole number^-?\d+$
Spaces at the start or end^\s+|\s+$

These patterns are good for finding text. They do not prove that an email address or website really exists, and no single pattern can check every valid email address. To make sure an address works, send a message to it.

Regex Differs Between Languages

This tester uses the JavaScript regex engine. The basics are the same in Python, PHP, Java and .NET, but some details are not:

  • Python writes named groups as (?P<name>...), while JavaScript uses (?<name>...).
  • JavaScript has no \A or \Z anchors, no possessive quantifiers such as a++, and no atomic groups.
  • Inline flags such as (?i) are not supported in JavaScript, so use the flag buttons instead.

Working with data in other formats? Try the JSON Formatter, the Text Diff Tool or the Unix Timestamp Converter.

Frequently Asked Questions

What is a regular expression?

A regular expression, or regex, is a short pattern that describes a piece of text. You can use it to find, check or replace text, for example to pull every email address out of a document or check that a field contains only digits.

Why does my regex only find the first match?

Without the Global (g) flag, JavaScript stops after the first match. Switch on the Global flag to find all matches in the text.

What do the flags g, i, m, s and u mean?

g finds all matches, i ignores upper and lower case, m makes ^ and $ work on every line, s lets the dot match line breaks, and u turns on full Unicode support, which helps with emoji and other special characters.

Why was my pattern stopped after 2 seconds?

Some patterns, such as (a+)+$, can take an enormous amount of time on certain text. This is called catastrophic backtracking. To keep your browser working, the tool stops any pattern that runs longer than 2 seconds. Use a simpler pattern, or avoid repeating a group that already repeats.

Will my regex work in Python, PHP or Java?

Most basic patterns work everywhere, but this tool uses the JavaScript engine, and other languages differ in small ways. For example, Python writes named groups as (?P<name>...), and JavaScript does not support \A, \Z, possessive quantifiers or inline flags such as (?i). Always test in the language you will use.

How do I match a line break or any character including new lines?

Turn on the Dot matches newline (s) flag so the dot matches line breaks too. You can also use [\s\S], which matches any character, including line breaks, without any flag.

Is my text sent to a server?

No. The pattern runs in your browser. Nothing you type is sent or stored.

Always FreeAll tools are 100% free to use.
No Sign UpNo registration or login required.
Blazing FastInstant results, always.