Search & Replace
Finding text is only half the story — the other half is replacing it. Every regex-capable language has a replace function that takes a pattern, the input and a replacement. The replacement can be plain text, a template that refers to the match, or a function that computes the new text.
Replacement templates in JavaScript
String.prototype.replace and replaceAll understand these special sequences in the replacement string:
| Sequence | Inserts |
|---|---|
amp; |
the whole match |
$1 … $99 |
the text of a numbered group |
lt;name> |
the text of a named group |
|
the text before the match |
#39; |
the text after the match |
$ |
a literal $ |
const s = "Call 0912345678 now";
console.log(s.replace(/\d+/, "[amp;]"));
console.log(s.replace(/(\d{4})(\d{3})(\d{3})/, "$1 $2 $3"));
console.log(s.replace(/\d+/, "$amp;"));
console.log("10 USD".replace(/(?<n>\d+) USD/, "lt;n> dollars"));Call [0912345678] now
Call 0912 345 678 now
Call $0912345678 now
10 dollars
Note: Without the
gflag,replacechanges only the first match.replaceAllrequires either a string or a regex withg— passing a non-global regex throws aTypeError.
Reformatting with groups
Groups let you rearrange pieces. Converting "Last, First" names to "First Last":
const names = "Tran, Mai\nNguyen, Bao\nLe, Thu";
console.log(names.replace(/^(\w+), (\w+)$/gm, "$2 $1"));Mai Tran
Bao Nguyen
Thu Le
Before running a replacement in code, it's worth checking what the pattern matches in the tester:
/^(\w+), (\w+)$/gm
Tran, Mai
Nguyen, Bao
Le, Thu3 matches
#1 "Tran, Mai" at index 0
group 1: "Tran"
group 2: "Mai"
#2 "Nguyen, Bao" at index 10
group 1: "Nguyen"
group 2: "Bao"
#3 "Le, Thu" at index 22
group 1: "Le"
group 2: "Thu"
Replacement functions
When the new text needs logic — arithmetic, lookups, case changes — pass a function. It receives the match, then each group, then the offset, the whole string and (if any) the named groups object:
const prices = "Coffee 30000, cake 45000";
const out = prices.replace(/\d+/g, (m) => (Number(m) / 1000).toFixed(0) + "k");
console.log(out);
const rates = { USD: 25000, EUR: 27000 };
const text = "5 USD + 2 EUR";
console.log(text.replace(/(\d+) (USD|EUR)/g, (_, n, cur) => `${n * rates[cur]} VND`));Coffee 30k, cake 45k
125000 VND + 54000 VND
Inserting at positions
Since lookarounds and anchors match empty positions, a replacement can insert text without removing anything:
console.log("helloWorldFooBar".replace(/(?<=[a-z])(?=[A-Z])/g, "_").toLowerCase());
console.log("line one\nline two".replace(/^/gm, " "));hello_world_foo_bar
line one
line two
Replacement syntax in other languages
| Language | Function | Group ref | Named ref | Whole match |
|---|---|---|---|---|
| JavaScript | s.replace(re, r) |
$1 |
lt;name> |
amp; |
| Python | re.sub(p, r, s) |
\1 or \g<1> |
\g<name> |
\g<0> |
| Java | s.replaceAll(p, r) |
$1 |
${name} |
$0 |
| C# | Regex.Replace(s, p, r) |
$1 |
${name} |
$0 |
| Go | re.ReplaceAllString(s, r) |
${1} |
${name} |
${0} |
| sed | s/p/r/g |
\1 |
— | & |
import re
s = "2025-07-31 and 2025-08-02"
print(re.sub(r"(\d{4})-(\d\d)-(\d\d)", r"\3.\2.\1", s))
print(re.sub(r"\d+", lambda m: str(int(m.group()) * 2), "3 apples, 10 pears"))
print(re.subn(r"\s+", " ", "a b \t c"))31.07.2025 and 02.08.2025
6 apples, 20 pears
('a b c', 2)
Tip: In Go, write
${1}xrather than$1x— Go reads$1xas a group named1x, which doesn't exist, and silently inserts nothing.
Test yourself
What does "ab".replace(/b/, "[amp;]") return?
a[amp;]a[b][ab]a[]
How do you insert a literal dollar sign in a JavaScript replacement string?
\$$$0amp;
Which Python replacement refers to a named group y?
lt;y>${y}\g<y>\k<y>