正規表現 -replace-

構文:  文字列.replace(正規表現オブジェクト,置き換え後文字列)
戻り値: 置き換えた後の文字列

console.log("012abc34def5678".replace(/\d+/,"数字"));    // "数字2abc34def5678"

gフラグ(複数回マッチ)

console.log("012abc34def5678".replace(/\d+/g,"数字"));    // "数字abc数字def数字
"

正規表現を()で囲むとグループ化できる
グループ化した正規表現にマッチした文字列の取得(キャプチャ) => $1(1番目のグループ)、$2(1番目のグループ)、・・・
$& はマッチした文字列を意味するので、正規表現を()で囲まなくてもよい

console.log("012abc34def5678".replace(/(\d+)/g,"L$1R"));     //  L012RabcL34RdefL5678R
console.log("012abc34def5678".replace(/\d+/g,"L$&R"));     //  L012RabcL34RdefL5678R

関数を使った置き換え

console.log("012abc34def5678".replace(/\d+/g,(str)=>{
	return "L"+ str + "R";
}));  // L012RabcL34RdefL5678R

関数の中でキャプチャした内容を使う
引数の2つ目以降にキャプチャした内容が入る
下記例では、strにマッチした文字列、f1に()でグループ化して取得したキャプチャ文字列が入る

console.log("aaa1234bcd567efg8999h".replace(/aaa(\d+)/g,(str,f1)=>{
	return "前"+ str + "後";
}));  // 前aaa1234後bcd567efg8999h

console.log("aaa1234bcd567efg8999h".replace(/aaa(\d+)/g,(str,f1)=>{
	return "前"+ f1 + "後";
}));  // 前1234後bcd567efg8999h

console.log("aaa1234bcd567efg8999h".replace(/aaa(\d+)/g,(str,f1)=>{
	return parseInt(f1) * 2;
}));  // 2468bcd567efg8999h

参考
キャプチャされた文字列を使用してリプレイ
https://uhyohyo.net/javascript/4_3.html