Regex Tester with Replace: Live Matches and Capture Groups

Test a JavaScript regex on your text: highlighted matches, positions, capture groups, replacement with $1 and $2, token explanations and ready-made patterns.

Understanding a regex: capture groups, $1 and replacement

Each pair of parentheses in a regular expression captures part of the match, numbered from left to right ($1, $2…), which the replacement can reuse. The tool highlights matches in the text, breaks down the captured groups of each one, names the syntax elements found in the pattern and gives you the equivalent JavaScript code.

Capture groups in practice

The default pattern ([a-zA-Z0-9._%+-]+)@([a-zA-Z0-9.-]+\.[a-zA-Z]{2,}) holds two groups: $1 captures the part before the at sign, $2 the domain. On the sample text, with the gi flags, the tool finds 3 addresses; for support@kristools.com, $1 is support and $2 is kristools.com. The replacement [$1 at $2] gives [support at kristools.com]. A group that starts with (?: groups without capturing and gets no number.

Example: turning ISO dates into day/month/year

The ISO date preset captures year, month and day in three groups. On its sample sentence it keeps 2026-08-25 and 2026-12-31 and skips 2026-13-40, an impossible month. The replacement $3/$2/$1 produces 25/08/2026 and 31/12/2026.

What the tool shows

  • Matches highlighted in the text, with their start and end positions.
  • For each match, the list of captured groups $1, $2…
  • An analysis naming the elements spotted in the pattern: ^, $, \d, \w, \s, \b, the quantifiers +, * and ?, groups and bracket classes.
  • The JS Code button, which copies const regex = /pattern/flags; followed by a call to text.match(regex).

The eight presets (email, URL, French phone number, ISO date, IPv4 limited to 0-255, hex colour, UUID v4, HTML tag) also load a matching sample text.

Limits

  • The analysis spots characters, it does not parse structure: a + inside a class such as [+-] is still described as a quantifier. Braces {n,m}, alternation |, the dot and lookahead or lookbehind assertions are not explained.
  • An unescaped forward slash in the pattern makes the copied JavaScript invalid; write \/.
  • With the g flag the list stops at 1,000 matches.

To mask emails, UUIDs or tags in a text, load the matching preset, type the replacement text and copy the output with the button above the resulting string.

Frequently Asked Questions

How do I use $1 and $2 in a regex replacement?

$1 inserts the text captured by the first pair of parentheses and $2 by the second, counting opening parentheses from left to right. With the default pattern and the replacement [$1 at $2], support@kristools.com becomes [support at kristools.com].

amp; inserts the whole match.

What is the difference between (...) and (?:...)?

(...) captures text and gives it a reusable number ($1, $2…). (?:...) only groups, for instance to apply a quantifier to several characters, without creating a numbered group. Turning an unneeded group into (?:...) keeps the numbering of the following groups unchanged.

How do I convert a YYYY-MM-DD date to DD/MM/YYYY with a regex?

Capture the three parts with \b(\d{4})-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])\b and replace with $3/$2/$1. 2026-08-25 becomes 25/08/2026, while 2026-13-40 is skipped because there is no month 13. Day 31 is still accepted for every month, so 2026-02-31 goes through.

Does the copied JavaScript code work as is?

For most patterns, yes: the tool copies const regex = /pattern/flags; and then text.match(regex). If the pattern contains an unescaped forward slash, the literal becomes invalid, so write \/ instead of /. With the g flag, match returns the list of matches without groups; use matchAll to get them.

Why does my regex find only one match?

The g (global) flag is probably off. Without it, JavaScript stops at the first match, for searching and for replacing alike. Turn g on to get every occurrence.

Which regex matches a French phone number?

The built-in pattern is (?:(?:\+|00)33|0)\s*[1-9](?:[\s.-]*\d{2}){4}. It matches both 06 12 34 56 78 and +33 1.23.45.67.89: a +33, 0033 or 0 prefix, one digit from 1 to 9, then four pairs of digits optionally separated by spaces, dots or hyphens.

How do I mask every email address in a text with a regex?

Load the email pattern, keep the g flag on and type [EMAIL HIDDEN] in the replacement field. On the sample text it loads, the 2 complete addresses are replaced and invalid@email, which has no domain extension, stays as it is. The copy button grabs the resulting text.

Does the IPv4 pattern check that an IP address is valid?

Yes, for the range of each octet: the pattern repeats the alternation 25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]? four times, which keeps every number between 0 and 255. 192.168.1.50 matches, 999.300.1.1 does not. It does not tell you whether the address is private or public.

Is this tool really free?

Yes, all tools on Kristools are 100% free and accessible without registration.

Is my data protected?

Yes. All processing is performed directly and securely inside your browser. No personal data is sent to our servers.