If you have looked at my other article, you already know how to replace all the matching string from a string variable. However, some times you will need to replace all the string values that are received from a variable.
This can be achieved by creating a Regular Expression object with the necessary variables and passing it to the regular replace method of the string. Below is the sample code.
This can be achieved by creating a Regular Expression object with the necessary variables and passing it to the regular replace method of the string. Below is the sample code.
var strToBeReplaced = "This 1 is a warning 2";
for (var k=1;k<3;k++)
{
strToBeReplaced = strToBeReplaced.replace(new RegExp(k, 'g'), "message");
}
alert(strToBeReplaced);
//shows "This message is a warning message"