Regex tester
Write a regular expression and see every match highlighted as you type, with capture groups and replace.
This tool runs in your browser. We never send what you type or paste to a server.
Quick reference
. | Any character except line breaks (all characters with the s flag) |
\d \w \s | Digit, word character, whitespace |
[abc] [^abc] | Any of a, b, c / anything except them |
* + ? {2,5} | 0+, 1+, 0 or 1, between 2 and 5 times |
^ $ | Start and end of input (of each line with m) |
( ) (?:) (?<name>) | Capture group, non-capturing group, named group |
a|b | a or b |
(?=) (?!) | Lookahead, negative lookahead |
Flavour
This tester uses JavaScript's regex engine, the same as browsers and Node.js. Most syntax works the same in Python, Java, PHP and .NET, but details such as lookbehind support and named-group syntax can differ.
Example
Pattern (\d{4})-(\d{2})-(\d{2}) with flag g, on the text Due 2026-10-26, paid 2026-11-02:
- Two matches, each with three groups: year, month and day.
- Replace with
$3/$2/$1givesDue 26/10/2026, paid 02/11/2026.
Named groups work too: (?<year>\d{4}) and then $<year> in the replacement.
How the engine finds a match
JavaScript, like Python, Java and PCRE, uses a backtracking engine. It tries the pattern at position 0, then 1, and so on, and reports the first position where the whole pattern succeeds (the leftmost match). Within that attempt, *, + and {n,m} are greedy: they take as much as they can and give characters back only if the rest of the pattern fails. Adding ? after them makes them lazy:
const html = '<b>bold</b>';
console.log(html.match(/<.+>/)[0]); // → <b>bold</b> greedy: as much as possible
console.log(html.match(/<.+?>/)[0]); // → <b> lazy: as little as possible
Giving characters back and retrying is what makes nested quantifiers like (a+)+ so slow on text that almost matches. Prefer a specific class such as <[^>]+> to <.+?>: it is clearer and cannot run past the closing bracket.
Flags
g | Find every match (and replace every match). Without it, the replace preview changes only the first one. |
i | Ignore case. |
m | ^ and $ match at each line break, not just the ends of the text. |
s | . also matches line breaks. |
u | Unicode mode: work on code points, allow \p{…} property escapes, and reject sloppy escapes. |
v | A stricter Unicode mode with set operations in classes, such as [\p{L}--[a-z]]. Use u or v, not both. |
y | Sticky: each match must start exactly where the previous one ended. |
d | Record start and end indices of each group (match.indices in code). |
See MDN's regular expressions guide for the full syntax.
Gotchas
- Backslashes in strings: the pattern
\d+has to be written"\\d+"inside a normal string in JavaScript, Java or C#. In Python use a raw string,r"\d+". Paste the pattern here without the extra escaping. \wand\bare ASCII only, even with theuflag, so\w+stops at the é in café. Use\p{L}withufor letters in any script.- Emoji are two units without
u, so.matches half of one. - A
gregex has state:test()andexec()continue fromlastIndex, so callingtest()twice on the same string can give true and then false.
console.log('café'.match(/\w+/)[0]); // → caf \w is ASCII only, even with u
console.log('café'.match(/\p{L}+/u)[0]); // → café
console.log(/^.$/.test('😀'), /^.$/u.test('😀')); // → false true without u an emoji is two units
const g = /a/g;
console.log(g.test('a'), g.test('a')); // → true false a g regex remembers lastIndex between calls
console.log('a.b.c'.replaceAll('.', '-')); // → a-b-c a plain string is not a pattern
The same pattern in other languages
JavaScript: matchAll needs the g flag, and replace also accepts a function.
const text = 'Due 2026-10-26, paid 2026-11-02';
const re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/g;
console.log([...text.matchAll(re)].map((m) => m.groups.day).join(' ')); // → 26 02
console.log(text.replace(re, '$<day>/$<month>/$<year>')); // → Due 26/10/2026, paid 02/11/2026
console.log(text.replace(re, (m, y, mo, d) => `${d}.${mo}.`)); // → Due 26.10., paid 02.11.
Python (re module) writes named groups as (?P<name>…) and references them as \g<name>. Note that re.match is anchored at the start.
text = 'Due 2026-10-26, paid 2026-11-02'
pat = r'(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})' # Python writes named groups as (?P<name>...)
print(re.findall(pat, text)[0]) # → ('2026', '10', '26') findall returns groups, not whole matches
print(re.sub(pat, r'\g<day>/\g<month>/\g<year>', text)) # → Due 26/10/2026, paid 02/11/2026
print(re.match(r'\d+', 'id 42')) # → None match() only tries at the start
print(re.search(r'\d+', 'id 42').group()) # → 42
Shell: grep -E and sed -E use POSIX extended syntax, which has no \d, lazy quantifiers or lookarounds; write [0-9], or use GNU grep -P.
echo 'Due 2026-10-26' | sed -E 's/([0-9]{4})-([0-9]{2})-([0-9]{2})/\3\/\2\/\1/' # → Due 26/10/2026
echo 'Due 2026-10-26' | grep -oE '[0-9]{4}-[0-9]{2}-[0-9]{2}' # → 2026-10-26
echo 'Due 2026-10-26' | grep -oP '\d{4}(?=-)' # → 2026 -P (GNU grep) for \d and lookarounds
PowerShell uses the .NET engine, which is case-insensitive by default with -match and -replace.
'Due 2026-10-26' -replace '(\d{4})-(\d{2})-(\d{2})', '$3/$2/$1' # → Due 26/10/2026 single quotes, or PowerShell expands $3 itself
'ABC' -match 'abc' # → True -match ignores case; -cmatch does not
if ('id 42' -match 'id (?<n>\d+)') { $Matches.n } # → 42Questions
What does the g flag do?
Global: find every match instead of stopping at the first.
How do I use a capture group in the replacement?
Write $1 for the first group, $2 for the second, or $<name> for a named group.
Why did matching stop with a "too slow" message?
Some patterns, such as (a+)+$, take exponentially longer as the text grows (catastrophic backtracking). The tester stops them after a quarter of a second so the page does not freeze. The same pattern would hang your server, so it is worth rewriting.
Why does my regex work here but not in Python, grep or my editor?
Each engine has its own dialect. Named groups are (?<name>) in JavaScript but (?P<name>) in Python, grep -E has no \d or lookarounds, and the replacement syntax differs ($1 in JavaScript and .NET, \1 or \g<1> in Python, \1 in sed). Test in the engine you will deploy to.
How do I match a literal dot, bracket or question mark?
Escape it with a backslash: \. \[ \? \( \$ and so on. Inside a character class most characters are literal, so [.] also matches a dot. In code, escaping user input before building a pattern prevents both errors and injected patterns.
How do I match text that spans several lines?
Turn on the s flag so that . also matches line breaks, or use [\s\S] in engines without it. If you want ^ and $ to match at the start and end of each line instead, that is the m flag.