Eskillsfy

Home / Blog / Text

Text · 23 Nov 2026

Find and Replace Text: how it works and how to build one

Find and replace in text, with regex and capture groups. This guide explains what happens behind the button and shows the code, so you can build your own.

What it does

Replace plain text or use a regular expression with $1-style capture groups, case matching and whole-word mode. \n in the replacement inserts a new line. Results update as you type.

When to use it

  • Bulk edits in exported data
  • Renaming things in a document
  • Reformatting dates and IDs with capture groups

How to use it

  1. Paste text or open a file.
  2. Type what to find and the replacement.
  3. Copy or download the result.

Try it free in your browser, nothing is uploaded: https://eskillsfy.com/tools/find-replace

How it works

Plain-text search is turned into a regex by escaping special characters, so the same replace code handles both modes. The g flag replaces every match, i ignores case, and \b on both sides limits it to whole words.

In regex mode, $1, $2 in the replacement insert capture groups. In plain mode, a function replacement stops $ from being treated specially.

Build it yourself

Here is the core of the tool. It has no dependencies unless noted, so you can paste it into any TypeScript or JavaScript project.

replace.ts (ts)

export function findReplace(text: string, find: string, replace: string, { regex = false, caseSensitive = false, wholeWord = false } = {}) {
  let pattern = regex ? find : find.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
  if (wholeWord) pattern = `\\b(?:${pattern})\\b`;
  const re = new RegExp(pattern, caseSensitive ? "g" : "gi");
  return regex ? text.replace(re, replace) : text.replace(re, () => replace);
}

findReplace("Order 1042", "Order (\\d+)", "#$1", { regex: true }); // "#1042"

Make it production-ready

  • Count matches before replacing.
  • Highlight matches in the input.

Questions

How do capture groups work?

With Regex on, (\d+) captures digits, and $1 in the replacement puts them back.

Try the Find and Replace Text

Free, runs in your browser, nothing is uploaded.

Open it
Keep reading

More in Text

All posts