DevKits

Regex Find and Replace — Online JavaScript Regex Replace Tool

Find and replace text with a regular expression online. Supports every JavaScript flag (g, i, m, s, u, y), all backreferences ($1..$9, $&, $`, $', $<name>), and escape sequences (\n, \t) in the replacement. Live match-by-match preview and diff. 100% local.

Last updated:

//g
179 chars · 4 lines
8 replacements
Match-by-match preview (showing first 8)
#1 @ 0

Match

John Smith
$1: John
$2: Smith

Replaced with

Smith, John
#2 @ 12

Match

Alice Johnson
$1: Alice
$2: Johnson

Replaced with

Johnson, Alice
#3 @ 27

Match

Bob Miller
$1: Bob
$2: Miller

Replaced with

Miller, Bob
#4 @ 61

Match

com and
$1: com
$2: and

Replaced with

and, com
#5 @ 96

Match

15 10
$1: 15
$2: 10

Replaced with

10, 15
#6 @ 117

Match

42 action
$1: 42
$2: action

Replaced with

action, 42
#7 @ 141

Match

15 10
$1: 15
$2: 10

Replaced with

10, 15
#8 @ 162

Match

42 action
$1: 42
$2: action

Replaced with

action, 42

Just want to test whether a pattern matches? Regex Tester → · Looking for a ready-made pattern? Regex Cookbook →

Enter a regex pattern, a replacement string, and (optionally) flags — the tool applies the replacement live and shows a per-match preview. Use $1..$9 or $<name> to reference capture groups, and \n / \t / \\ to inject newlines, tabs, or literal backslashes in the replacement.

What is Regex Replace?

Regex find-and-replace is what makes regular expressions genuinely productive — one small pattern can rewrite thousands of lines. This tool follows the exact rules of JavaScript's String.prototype.replace(regex, replacement): all six flags (g, i, m, s, u, y) are supported, and every replacement-token JS supports is honored — $1 through $99 for numbered groups, $<name> for named groups, $& for the whole match, $` and $' for the pre/post-match context, and $$ for a literal dollar. Beyond raw JS, this tool also expands \n, \t, \r, and \\ in the replacement string so 'insert a newline' works the natural way, and it emits a per-match trace panel so you can spot 'why did that one line come out wrong' without instrumenting your own code.

How to find and replace with regex online

  1. 1Type your pattern in the Pattern field (no wrapping slashes needed).
  2. 2Toggle flags — 'g' (global) is on by default so every match is replaced.
  3. 3Enter the replacement, using $1..$n for capture groups, $<name> for named groups, and \n / \t for newlines / tabs.
  4. 4The output pane and match-detail panel update live. Copy the output when you're done.

Use Cases

Reformat log lines

Turn `2026-01-15 10:00 [INFO] user=42 action=login` into `[login] user=42` with a single capture-group replacement.

Anonymize sensitive data

Redact emails or credit-card numbers with `[REDACTED]` in one paste — safer than doing it by hand across a large log.

Rename identifiers across a file

Rename `oldName` → `newName` while preserving surrounding punctuation, or convert snake_case to camelCase with `_([a-z])` → `$1.toUpperCase()`-like expansions.

Convert data formats

Reshape CSV to JSON-ish structure, or reorder columns, using capture groups. Faster than opening a spreadsheet for small ad-hoc edits.

Code Examples

Same behavior in Node.js

// Rewrite "John Smith" → "Smith, John"
const out = input.replace(/(\w+) (\w+)/g, "$2, $1");

Equivalent in Python (re.sub)

import re
out = re.sub(r"(\w+) (\w+)", r"\2, \1", input)  # \1 \2, not $1 $2

sed equivalent

# GNU sed - Extended regex with -E; & is the whole match, \1..\9 are groups
sed -E 's/(\w+) (\w+)/\2, \1/g' file.txt

Key Concepts

Backreference
In the replacement string, `$1` inserts what capture group 1 matched. Named groups use `$<name>`. In the pattern itself, use `\1` to require a repeated match.
Escape sequences in replacement
Unlike the pattern, JavaScript's replacement string does not natively interpret \n or \t. This tool expands them for you so typing `\n` inserts an actual newline.
Global vs single replace
The `g` flag replaces every match. Without `g`, only the first match is replaced — the standard behavior of `String.replace`.

Tips & Best Practices

  • Test the pattern first in the Regex Tester if you're unsure it matches what you think — 'wrong pattern' is a more common bug than 'wrong replacement'.
  • For huge replacements across many capture groups, name the groups (`(?<year>\d{4})`) and reference them by name — the intent is easier to read months later.
  • If your replacement text contains a literal `$`, escape it as `$$`.
  • Regex replace is not the right tool for HTML or JSON transforms — use a parser. Regex is fine for CSV, log, and plain-text transforms.
  • Enable case-insensitive `i` for text tasks like 'replace every occurrence of company name' — English text mixes case unpredictably.

Frequently Asked Questions

Which backreference syntax is supported in the replacement?

All JavaScript replacement patterns: $1..$99 for numbered groups, $<name> for named groups, $& for the entire match, $` for the text before, $' for the text after, and $$ for a literal dollar sign.

How do I insert a newline or tab in the replacement?

Type \n for newline, \t for tab, \r for carriage return, or \\ for a literal backslash. Escape expansion is on by default — flip it off if you need those sequences kept literal.

Does it replace only the first match or all of them?

It follows the flags you set. Add the 'g' flag for all matches (typical sed-like behavior) or omit it to replace only the first match.

What happens if my pattern is invalid?

You'll see the exact SyntaxError message from the JavaScript engine (unmatched paren, invalid escape, etc.) and the input is left untouched. Fix the pattern to try again.

Is my text uploaded anywhere?

No. Everything runs locally in your browser — regex evaluation, replacement, and diff computation.

Related Tools