How-to Guide 5 min read

Find and Replace With Regex: Capture Groups, Whole Words, Examples

Regex find and replace online: whole-word matching, capture groups with $1 and $2, swapping names, reformatting dates and cleaning spaces, with examples.

find and replace regexregex capture group replaceregex replace onlinewhole word replacefind and replace text online

The short answer

To find and replace with a regular expression, tick Use regex, put a pattern in Find and use $1, $2 and so on in Replace with to reuse the parts of each match you captured in brackets. For example, Find (\d{4})-(\d{2})-(\d{2}) and Replace $3/$2/$1 turns 2026-10-17 into 17/10/2026. The free Find and Replace Text tool shows the result and the number of matches as you type.

The four options and what they do

The tool has four switches. Understanding them prevents most surprises.

OptionWhat it does
Case sensitiveOff: "cat" also matches "Cat" and "CAT". On: only exact case matches.
Whole wordMatches only complete words, so "cat" no longer matches inside "category".
Use regexTreats Find as a regular expression instead of plain text.
Replace allOn: every match is replaced. Off: only the first match is replaced.

The match counter always shows all matches in the text, even when Replace all is off and only the first one is changed. So "3 matches found" with Replace all off means one replacement was made and two were left.

Plain-text replace: the whole-word trap

Replacing "cat" with "dog" in plain text mode:

  • Text: The cat sat on the category mat.
  • Result: The dog sat on the dogegory mat. (2 matches)

Ticking Whole word fixes it:

  • Result: The dog sat on the category mat. (1 match)

Turn on Whole word whenever you replace a short word that can appear inside longer ones, such as "in", "art" or a product code.

With Use regex off, characters such as ., *, ( and $ in Find are treated as normal characters. Replacing "." with "-" in a.b.c gives a-b-c. With regex on, . means "any character", and the same replacement turns all five characters into dashes.

Capture groups: reuse parts of the match

Brackets in a regex pattern "capture" whatever they match. In the replacement, $1 is the first captured part, $2 the second, and so on. $& means the whole match.

Example 1: Reformat dates

  • Find: (\d{4})-(\d{2})-(\d{2})
  • Replace with: $3/$2/$1
  • Text: 2026-10-17 and 2025-01-05
  • Result: 17/10/2026 and 05/01/2025

\d{4} means exactly four digits. The three groups are year, month and day, which the replacement puts back in a new order.

Example 2: Swap "Surname, First name"

  • Find: (\w+), (\w+)
  • Replace with: $2 $1
  • Text: two lines, Kumar, Ravi and Sharma, Asha
  • Result: Ravi Kumar and Asha Sharma

\w+ matches one or more letters, digits or underscores. Note that \w does not match accented or non-Latin letters, so a name like "Zoë" or "रवि" needs a different pattern, such as ([^,]+), (.+).

Example 3: Add text around every match

  • Find (plain text): 10
  • Replace with: $&.00
  • Text: price: 10
  • Result: price: 10.00

Everyday clean-up patterns

These patterns are tested in the tool with Use regex and Replace all on.

TaskFindReplace with
Collapse repeated spaces into one\s+a single space
Join lines with commas\n,
Remove all digits\d(leave empty)
Delete text in brackets\s*\([^)]*\)(leave empty)
Keep only the first word before a dash\s*-.*(leave empty)

Be careful with \s+: it matches line breaks too, so it will also join all your lines into one. To collapse spaces but keep lines, use [ \t]+ instead.

For patterns that validate formats such as mobile numbers, PIN codes or PAN, see regex for Indian mobile numbers, PIN codes and PAN, and test them first in the Regex Tester.

Things that commonly go wrong

^ and $ match only the start and end of the whole text

In this tool, ^ means the very start of the text and $ the very end, not the start and end of each line. So Find ^(\w+), (\w+)$ on two lines finds 0 matches. Remove ^ and $, as in Example 2, and both lines match.

A dollar sign in the replacement behaves specially

Even in plain-text mode, the replacement treats $&, $1 and similar sequences as special. If you want a literal "$1" in your output, write $$1. A dollar sign followed by a space or a letter is left as it is, so "₹" and "$ 5" type replacements work normally.

You cannot type a new line in the replacement

The Replace with box is a single line, and typing \n inserts the two characters backslash and n, not a line break. To split text onto new lines, do the replacement with a marker such as |||, then paste the result into an editor that supports line-break replacement. Joining lines (Find \n) does work, because the Find box understands regex escapes.

"Invalid regular expression" error

An unclosed bracket, such as Find (, shows an error message in red and leaves the text unchanged. To match a literal bracket or dot in regex mode, put a backslash before it: \(, \..

A safe workflow for big replacements

  • Paste a short sample first and check the result panel.
  • Watch the match count: if it is much higher than you expect, the pattern is too broad.
  • Turn on Whole word or make the pattern more specific.
  • Paste the full text and copy the result.
  • Compare before and after with the Text Diff Checker if the text is important.
  • The replacement runs in your browser as you type, so you can adjust the pattern and see the effect immediately.

    Frequently asked questions

    How do I use capture groups in find and replace?

    Turn on Use regex, wrap the parts you want to keep in brackets in the Find pattern, and refer to them as $1, $2 and so on in Replace with. $& inserts the whole match.

    How do I replace a word without changing longer words that contain it?

    Turn on Whole word. Replacing cat with dog then changes "the cat" but leaves "category" alone.

    Why does my regex with ^ and $ find no matches?

    In this tool ^ and $ match the start and end of the whole text, not each line. Remove them, or write the pattern so it matches within each line without anchors.

    Can I replace text with a new line?

    Not directly. The replacement box is single-line and \n is inserted as literal characters. You can join lines by finding \n in regex mode, but to split lines use a marker and finish in a text editor.

    Why does the match count not change when Replace all is off?

    The counter always shows every match in the text. With Replace all off, only the first match is actually replaced.

    Try this tool on Lazyblink

    Put this guide into practice with our free online tool — no signup required.

    Open tool