The short anser is:
var world = "World";
var myString = "Hello World!";
myString.replace(new RegExp(`Hello ${world}`), "Hello Earth");
// Outputs "Hello Earth!"
If the content of the string you are searching for is coming as input from the user, you might want to escape it first, so the user cannot insert special RegExp characters and manipulate your regular expression in ways you might not expect.
// Escape special RegExp characters
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
var world = "World";
world = escapeRegExp(world);
var myString = "Hello World!";
myString.replace(new RegExp(`Hello ${world}`), "Hello Earth");
// Outputs "Hello Earth!"