您可以传递一个函数来替换:
var hello = "Hello World what a beautiful day";hello.replace(/Hello|World/g, function ($0, $1, $2) // $3, $4... $n for captures{ if ($0 == "Hello") return "Bye"; else if ($0 == "World") return "Universe";});// Output: "Bye Universe what a beautiful day";


