How to test a regular expression
- Enter your pattern in the Regex pattern box without the surrounding slashes, for example ^\d{3}-[A-Z]{2}$, and tick the flags you need: g, i, m, s, u or y.
- Type or paste the text to search into the Test String box, open a text file, or select Load example or one of the Common Regex Examples.
- Read the results, which update as you type: every match is highlighted, and Match Results lists each match with its start and end index and its capture groups.
- Check the Regex Explanation to see what each token does, and the Regex Debugger for syntax errors, suggested fixes and backtracking warnings.
- Use Regex Replace to preview a replacement, Generate Test Data for sample strings, and Generate Regex Code to copy working code for JavaScript, C#, Python, Java or PHP.
Regex Cheat Sheet
A regular expression (regex) is a pattern that describes text. This regex tester runs your pattern with the
JavaScript RegExp engine built into your browser, so every match, index and capture group is exactly what
regex.exec(), String.match() and String.replace() return in JavaScript. The pattern is
also parsed separately to explain each token, to find the cause of syntax errors and to look for patterns that can backtrack
excessively. Matching runs in a background worker that is stopped after two seconds, so a slow pattern cannot freeze the page.
Character classes
| Token | Matches |
|---|---|
. | Any character except line terminators (\n, \r,
,
) in the default mode; any character with the s flag |
\d | A digit 0–9 |
\w | A word character: A–Z, a–z, 0–9 or _ |
\s | A whitespace character: space, tab, line breaks and Unicode spaces |
\D, \W, \S | Any character that is not a digit, word character or whitespace |
[abc] | One of a, b or c |
[^abc] | Any character except a, b or c |
[a-z] | One character in the range a to z |
\p{L} | A Unicode letter in any script (needs the u flag) |
Quantifiers
| Token | Repeats the previous item |
|---|---|
* | Zero or more times |
+ | One or more times |
? | Zero or one time (optional) |
{n} | Exactly n times |
{n,} | n or more times |
{n,m} | Between n and m times |
*?, +?, ??, {n,m}? | Lazy versions: as few times as possible instead of as many |
Anchors
| Token | Matches the position |
|---|---|
^ | At the beginning of the string, or of each line with the m flag |
$ | At the end of the string, or of each line with the m flag |
\b | At a word boundary, between a word character and a non-word character |
\B | Anywhere that is not a word boundary |
Groups, alternation and lookarounds
| Token | Meaning |
|---|---|
(...) | Capturing group: groups items and saves the matched text as a numbered group |
(?:...) | Non-capturing group: groups items without saving the text |
(?<name>...) | Named capturing group, where supported (all current browsers) |
\1, \k<name> | Backreference: the same text a group captured earlier |
a|b | Alternation: a or b |
(?=...), (?!...) | Lookahead: what follows must, or must not, match |
(?<=...), (?<!...) | Lookbehind: what precedes must, or must not, match |
Flags
| Flag | Effect |
|---|---|
g | Global: find all matches, not just the first |
i | Ignore case |
m | Multiline: ^ and $ match at line breaks |
s | DotAll: . also matches line breaks |
u | Unicode: code point matching, \u{…} and \p{…}, stricter syntax |
y | Sticky: match only at lastIndex |
Escapes and replacement tokens
Put a backslash before a special character to match it literally: \., \*, \?,
\(, \[, \{, \|, \\. \t, \n and
\r match a tab, line feed and carriage return, and \xHH and \uHHHH match a character by
its code. In a replacement, $1 inserts group 1, $<name> a named group, $& the
whole match and $$ a dollar sign.
Regex tester example
Suppose invoice numbers look like INV-2026-AB. Enter the pattern \bINV-\d{4}-[A-Z]{2}\b with the
g flag, and this test string:
Paid INV-2026-AB and INV-1937-XK.
Pending: INV-99-ZZ, inv-2026-ab
Match Results reports 2 matches: INV-2026-AB at index 5–16 and INV-1937-XK at index
21–32. INV-99-ZZ is rejected because it has two digits instead of four, and inv-2026-ab because it is
lower case; tick i to accept it. The Regex Explanation lists \b as a word boundary, INV-
as literal text, \d and {4} as exactly four digits and [A-Z] and {2} as two
upper case letters. Generate Test Data produces strings such as INV-4821-QP, and Generate Regex Code gives, for C#:
var regex = new Regex(@"\bINV-\d{4}-[A-Z]{2}\b", RegexOptions.None, TimeSpan.FromSeconds(1));
foreach (Match match in regex.Matches(text))
{
Console.WriteLine("Match \"" + match.Value + "\" at index " + match.Index);
}
What developers use it for
- Checking a validation pattern for email addresses, phone numbers, PIN codes or IDs against valid and invalid samples before it goes into a form or an API.
- Extracting values such as dates, order numbers or IP addresses from log files and exports, and confirming that each capture group holds the right part.
- Debugging a regex copied from Stack Overflow or from another language, by seeing what each token means and why the browser rejects it.
- Previewing a search and replace, such as turning YYYY-MM-DD dates into DD/MM/YYYY, before running it on real data.
- Creating unit test fixtures with the test data generator, and porting a JavaScript regex to C#, Python, Java or PHP.
- Spotting nested quantifiers that could slow a server down (ReDoS) before the pattern reaches production.
Why a regex does not match
- Only the first match is found
- Without the g (global) flag, JavaScript stops at the first match, just like regex.exec(). Tick g to list every match; the results panel says when this applies.
- ^ and $ do not match at the start and end of each line
- By default ^ and $ only match at the start and end of the whole text. Tick m (multiline) so that they also match next to every line break.
- The dot does not match line breaks
- In JavaScript the dot excludes \n, \r, \u2028 and \u2029. Tick s (dotAll), or use [\s\S] to match any character including line breaks.
- The pattern works here but not in my code
- Inside a quoted string every backslash must be doubled, so new RegExp("\\d+") equals /\d+/. Other languages also differ in details; Generate Regex Code converts the syntax and lists the differences that remain.
- The test stopped after 2 seconds
- Matching took too long on this text, usually because of catastrophic backtracking from nested quantifiers such as (a+)+. Read the Regex Performance Check and make the repeated parts unambiguous.
Frequently Asked Questions
What is a regex tester?
A regex tester is an online tool that runs a regular expression against sample text and shows what it matches. This one highlights every match, lists positions and capture groups, explains each token, points out syntax errors with suggested fixes, and generates test data and code, all in your browser.
How do I test a regular expression?
Enter the pattern in the Regex pattern box without slashes, choose flags such as g for all matches and i to ignore case, and paste your text into Test String. Matches are highlighted as you type and Match Results lists each one with its position and groups. Test both text that should match and text that should not.
What is the difference between a regex tester and a regex generator?
A regex tester checks an existing pattern against text. A regex generator produces something from a pattern: here, sample strings that the pattern matches (Generate Test Data) and ready-to-run code for other languages (Generate Regex Code). The tool does not invent a pattern from examples; the Common Regex Examples give tested starting points instead.
Which regex flavor does this tester use?
JavaScript (ECMAScript), run by your browser's own RegExp engine, so the results are exactly what JavaScript code in that browser returns. Other languages differ in details such as named group syntax, lookbehind limits and what \d matches; the code generator converts the syntax and lists the differences.
Why does my regex only match once?
The g flag is off. Without it JavaScript returns only the first match, as regex.exec() and text.match() do. Tick g to list every match.
What is catastrophic backtracking?
Some patterns, such as (a+)+$ or (\w+\s?)*$, can match the same text in a huge number of ways. When the text almost matches, the engine tries them all, which can take seconds or hang a server (a ReDoS risk). The performance check warns about common shapes, and tests run in a background worker that is stopped after 2 seconds so the page never freezes. The check is a guide, not a security guarantee.
Are the email, URL and password examples perfect validators?
No. They are practical patterns that accept common valid input and reject obvious mistakes. Standards such as RFC 5322 for email allow forms that simple patterns reject, so combine a regex with other checks, such as a confirmation email.
Is my regex or test text uploaded?
No. The pattern and the text are processed locally in your browser, on the page and in a background worker loaded from this site, and they are not sent to our server or to any other service.