Javascript Global Replace with Variable

Sometimes Javascript Global Replace with Variable can be difficult for you.

You can handle with new RegExp.

This is wrong way. Don’t waste your time such codes
[js]
var string = "aaaaa";
var oldLetter = "a";
string = string.replace(/oldLetter/g, newLetter); /* wrong way */
[/js]

You can change only one letter with with this way
[js]
var string = "aaaaa";
var oldLetter = "a";
var newLetter = "b";
string = string.replace(oldLetter, newLetter);
/*
Output: "baaaa"
*/
[/js]

This is true way
[js]
var string = "aaaaa";
var oldLetter = "a";
var newLetter = "b";
string = string.replace(new RegExp(oldLetter, "g"), newLetter);
/*
Output: "bbbbb"
*/
[/js]