Regular expression
g (global): find every match, not only the first.
$& whole match · $1 group 1 · $<name> named group · $` text before the match · $' text after it · $$ a $ sign
JavaScript syntax. Press an entry to insert it at the cursor. Esc closes this panel.
Characters
Classes
Anchors
Quantifiers
Groups
Lookaround
Uses this browser's JavaScript regex engine, the same one your JavaScript code runs on. Python, Java and Go differ in places: see below.
Test text
Result
match next match
Type a pattern and some test text. Matches appear here as you type.
Matches
Ctrl+Enter run / or Ctrl+K search tools Press Ctrl+D to bookmark this page
How to test a regular expression
- Type your pattern between the slashes. Write it as you would in a JavaScript regex literal, without the slashes.
- Turn flags on or off with the buttons under the pattern. Each one explains itself when you focus or press it.
- Paste or type your test text, or drop a text file on it. Matches are highlighted in the Result box as you type, and the Matches list shows each one with its line, column, index and capture groups.
- Switch to Replace to see the text after
replace(), or to List to write one line per match from a template such as$1,$2. - Use Copy /…/ or Copy new RegExp to take the pattern into your code with the escaping done.
If the pattern has a mistake, the Result box says what is wrong and, for common mistakes such as an unclosed group, points to the column. The browser's own message is shown too, so you see exactly what your code would report.
The eight flags
| Flag | What it does | Example |
|---|---|---|
g | Find every match, not only the first. Needed for matchAll() and for replace() to change every match. | /a/g on "banana" finds 3 matches |
i | Ignore case, so A and a are the same letter. | /cat/i matches "Cat" |
m | ^ and $ match at the start and end of every line, not only of the whole text. | /^\d+/gm finds a number at the start of each line |
s | A . also matches line breaks. | /a.b/s matches "a", a line break, then "b" |
u | Reads the pattern as Unicode code points: an emoji is one character, and \p{L} works. The syntax is stricter. | /^.$/u matches "😀"; without u it does not |
y | Sticky: each match must start exactly where the last one ended. | /\d/gy on "12a3" finds 1 and 2, then stops at "a" |
d | Also reports where each capture group starts and ends (match.indices). | Group start and end appear in the match list |
v | Like u, plus set operations and string properties in classes. Cannot be combined with u. | /[\p{L}--[a-z]]/v matches letters other than a to z |
Flags this browser does not support are greyed out. The d and v flags are the newest, so older browsers may not have them.
Capture groups and named groups
Parentheses capture the part of the match inside them. Groups are numbered from 1 in the order their opening ( appears, so in (\d{4})-(\d{2}) the year is group 1 and the month is group 2. A named group, (?<year>\d{4}), can also be read by name: match.groups.year in code, and $<year> in a replacement.
Use (?:…) when you only need to group, for example to repeat a part, and do not need what it matched. A group that took no part in the match, such as the second group in (a)|(b) when the text is "a", shows as "not matched" in the list. In code its value is undefined.
Greedy and lazy quantifiers
Quantifiers such as *, + and {2,5} are greedy: they take as much as they can, then give back only what the rest of the pattern needs. On the text <b>bold</b>, the pattern <.+> matches the whole thing, from the first < to the last >.
Add ? to make a quantifier lazy, so it takes as little as possible: <.+?> matches <b> and </b> separately. Often a more specific class is clearer and faster still: <[^>]+> can never run past a >.
Lookahead and lookbehind
A lookaround checks what comes next to the match without making it part of the match. \d+(?= USD) matches the number in "40 USD" but not the currency, and \d+(?! USD) matches a number that is not followed by " USD". Lookbehind looks to the left: (?<=\$)\d+ matches the digits in "$40", and (?<!\$)\d+ matches digits with no $ before them.
JavaScript allows lookbehind of any length, which many other languages do not. If you plan to use the same pattern in Python or Java, keep the lookbehind a fixed length.
Replacement references
| Write | To insert |
|---|---|
$& | The whole match |
$1 to $99 | A numbered group. If the group took no part, nothing is inserted. |
$<name> | A named group |
$` | The text before the match |
$' | The text after the match |
$$ | A single $ sign |
A $ followed by anything else is kept as it is, and so is $3 when the pattern has fewer than three groups. List mode uses the same references for each line it writes.
Test each line separately, or the m flag?
They look alike but work differently. The m flag still runs the regex once over the whole text. It only changes ^ and $ so they also match at line breaks, and a match can still run across lines, for example with \s or the s flag.
Test each line separately runs the regex once for every line, as if each line were its own input. Use it to check a list of values with a validation pattern, such as one email address or postcode per line. Each line is marked "matches" or "no match", with a count and a filter for the lines that fail. Windows line endings are handled, so a hidden \r never breaks a $.
Catastrophic backtracking
JavaScript's regex engine, like those of Python, Java and most other languages, backtracks: when part of a pattern fails, it goes back and tries another way. For most patterns that is fast. But a pattern such as ^(a+)+$, run on a long line of as that ends in b, has an enormous number of ways to split the text, and the engine tries them all before it gives up. Each extra character roughly doubles the time.
In your own code, that freezes the program or the browser tab, and on a server it can be used to slow the service down (known as ReDoS). Here the regex runs in a background worker that is stopped after 1.5 seconds, so the page stays usable and you get a message instead. A general warning also appears when a pattern repeats a group that itself repeats.
To avoid it, do not nest open-ended repetition such as (\w+\s?)+, make each part as specific as you can, and anchor patterns that should match the whole text. Go's regex engine (RE2) never backtracks, but it also has no lookaround or backreferences.
JavaScript compared with Python, Java and Go
Most everyday syntax, such as classes, quantifiers, groups and anchors, works the same everywhere. These are the differences that most often break a pattern when you move it between languages.
| Feature | JavaScript | Python (re) | Java | Go (RE2) |
|---|---|---|---|---|
| Named group | (?<name>…) | (?P<name>…) | (?<name>…) | (?P<name>…) |
| Group in a replacement | $<name>, $1 | \g<name>, \1 | ${name}, $1 | ${name}, ${1} |
| Lookbehind | Yes, any length | Fixed length only | Bounded length | No |
| Backreferences (\1) | Yes | Yes | Yes | No |
| Atomic groups and possessive quantifiers | No | Yes (3.11 and later) | Yes | No |
| Can backtrack badly | Yes | Yes | Yes | No: always linear time |
| Replace every match | Needs the g flag (or replaceAll) | sub() does by default | replaceAll() | ReplaceAllString() |
Escaping also differs. In a Java or JSON string, and in a JavaScript string passed to new RegExp, every backslash must be written twice. A Python raw string (r"\d+") and a Go back-quoted string do not need it. The Copy new RegExp button does the JavaScript escaping for you.
Your pattern and text stay in your browser
Matching runs on your device, in a background worker. Nothing you type is sent to a server. If "Remember my last input" is on in Settings, your pattern, options and test text are kept in this browser only, so they are back when you return. Clear all removes them.
Shared links include your pattern only if you tick that option, and the test text only if you also tick that one. They go after the #, which browsers never send to a server, and are removed from the address bar when the link is opened.
Frequently asked questions
Which regex flavor does this tester use?
JavaScript, using your browser's own RegExp engine, so results match what your JavaScript code does in this browser. Python, Java, PHP and Go differ in places, such as how named groups and replacements are written. The page lists the main differences.
Why does my pattern only find the first match?
Without the g (global) flag, JavaScript stops after the first match, and replace() changes only the first one. Turn on g. The tester also tells you how many matches g would find.
Why does test() return true, then false, for the same text?
A regex with the g or y flag remembers where its last match ended (lastIndex) and starts the next search there. Calling test() twice on the same text therefore fails the second time. Create the regex without g for test(), or set lastIndex back to 0.
What does "This pattern took too long to run" mean?
The pattern made the regex engine backtrack through a huge number of ways to match, often because a repeated group contains another repetition, such as (a+)+. The tester stops a run after 1.5 seconds so the page never freezes. In your own code the same pattern could hang the program.
What is the difference between the m flag and Test each line separately?
The m flag runs the regex once over the whole text and makes ^ and $ match at every line break. Test each line separately runs the regex on each line as its own string, so you can see which lines of a list pass a validation pattern.
Why does my pattern fail with the u flag but work without it?
With u or v, the syntax is stricter: escapes such as \a or \- outside a class, and a lone { or }, are errors instead of plain characters. Remove the backslash, or escape the brace as \{.
Is my pattern or text sent to a server?
No. Matching runs in your browser, in a background worker. If Remember my last input is on, your pattern and text are kept in this browser only; Clear all removes them. Shared links include them only if you choose.
Regular expressions in your code
Finding every match with named groups and replacing them, in Java, JavaScript, Python and Go, using each standard library. Each one also shows a trap that is specific to that language.
Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexDemo {
public static void main(String[] args) {
String text = "Order 1042 shipped on 2026-11-24 and arrives on 2026-11-27.";
// In a Java string every backslash is written twice: "\\d" is the regex \d.
Pattern date = Pattern.compile("(?<year>\\d{4})-(?<month>\\d{2})-(?<day>\\d{2})");
Matcher m = date.matcher(text);
while (m.find()) {
System.out.println(m.group() + " year=" + m.group("year")
+ " month=" + m.group("month") + " day=" + m.group("day"));
}
// A named group in the replacement is ${name}.
System.out.println(date.matcher(text).replaceAll("${day}/${month}/${year}"));
// matches() must match the whole text; find() looks for a match anywhere.
System.out.println(Pattern.matches("\\d+", "Order 1042") + " " + Pattern.compile("\\d+").matcher("Order 1042").find());
}
} java.util.regex, standard library only. Named groups need Java 7 or later.
JavaScript
// Regular expressions in JavaScript: the same engine this tester uses.
const text = 'Order 1042 shipped on 2026-11-24 and arrives on 2026-11-27.';
const date = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/g;
// matchAll needs the g flag and gives every match with its groups.
for (const m of text.matchAll(date)) {
const { year, month, day } = m.groups;
console.log(`${m[0]} year=${year} month=${month} day=${day}`);
}
// With g, replace() changes every match, not only the first.
console.log(text.replace(date, '$<day>/$<month>/$<year>'));
// Careful: a regex with g remembers lastIndex between test() calls.
const digits = /\d+/g;
console.log(digits.test('42'), digits.test('42'));
// In a string for new RegExp, every backslash is written twice.
console.log(new RegExp('\\d{4}').source); The same engine as this tester. Named groups and matchAll need a 2020 or later runtime.
Python
# Regular expressions in Python, standard library only.
import re
text = "Order 1042 shipped on 2026-11-24 and arrives on 2026-11-27."
# Python writes a named group as (?P<name>...).
date = re.compile(r"(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})")
for m in date.finditer(text):
print(f"{m.group(0)} year={m['year']} month={m['month']} day={m['day']}")
# sub() replaces every match by default. A named group in the
# replacement is \g<name>, not $<name> as in JavaScript.
print(date.sub(r"\g<day>/\g<month>/\g<year>", text))
# match() only looks at the start of the text; search() looks anywhere.
print(re.match(r"\d+", "Order 1042") is None, re.search(r"\d+", "Order 1042").group()) The re module. Named groups are written (?P<name>...).
Go
// Regular expressions in Go, standard library only.
package main
import (
"fmt"
"regexp"
)
func main() {
text := "Order 1042 shipped on 2026-11-24 and arrives on 2026-11-27."
// Go uses RE2: no lookaround and no backreferences, but matching
// always takes linear time, so it can never backtrack badly.
date := regexp.MustCompile(`(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})`)
for _, m := range date.FindAllStringSubmatch(text, -1) {
year := m[date.SubexpIndex("year")]
month := m[date.SubexpIndex("month")]
day := m[date.SubexpIndex("day")]
fmt.Printf("%s year=%s month=%s day=%s\n", m[0], year, month, day)
}
// Write groups in the replacement as ${name}: a bare $name takes
// every letter, digit and _ that follows as part of the name.
fmt.Println(date.ReplaceAllString(text, "${day}/${month}/${year}"))
// A lookbehind such as (?<=\$)\d+ is rejected by RE2.
_, err := regexp.Compile(`(?<=\$)\d+`)
fmt.Println(err != nil)
} The regexp package (RE2): no lookaround or backreferences, always linear time.
Last verified . Each example is run automatically and its output checked.