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:
Match
John SmithJohnSmithReplaced with
Smith, JohnMatch
Alice JohnsonAliceJohnsonReplaced with
Johnson, AliceMatch
Bob MillerBobMillerReplaced with
Miller, BobMatch
com andcomandReplaced with
and, comMatch
15 101510Replaced with
10, 15Match
42 action42actionReplaced with
action, 42Match
15 101510Replaced with
10, 15Match
42 action42actionReplaced with
action, 42Just 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
- 1Type your pattern in the Pattern field (no wrapping slashes needed).
- 2Toggle flags — 'g' (global) is on by default so every match is replaced.
- 3Enter the replacement, using $1..$n for capture groups, $<name> for named groups, and \n / \t for newlines / tabs.
- 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 $2sed equivalent
# GNU sed - Extended regex with -E; & is the whole match, \1..\9 are groups
sed -E 's/(\w+) (\w+)/\2, \1/g' file.txtKey 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
Regex Tester
Test regular expressions online. Live match highlighting, capture groups, and support for JavaScript flags (g, i, m, s, u, y).
Text to Speech
Convert text to spoken audio using your browser's built-in Speech Synthesis API. Pick from all voices installed on your OS, adjust rate, pitch, and volume. No account, no download, no upload — 100% local.
XML Formatter
Format, beautify, and validate XML documents online. Handles namespaces, CDATA sections, and mixed content.
SQL Formatter
Format and beautify SQL queries. Supports SELECT, JOIN, subqueries, CTEs, and multiple SQL dialects.
Case Converter
Convert text between camelCase, PascalCase, snake_case, kebab-case, CONSTANT_CASE, Title Case, and more.
Text Diff
Compare two text snippets side-by-side and highlight the differences. Word- and line-level diff, everything runs in your browser.