I want to replace all occurrences that match a certain predefined pattern.
Here's an example:
var str = "Mr Blue has a blue house and a blue car & listens to blues!";
var replaceStr = "blue"; // dynamically fetched
var pattern = new RegExp(replaceStr, "g");
alert(str.replace(pattern, "red"));
This however, also replaces "blues" to "reds" which was not intended, so explored a bit on word search using \b.
This works when I define the replaceStr as /blue\b/, however how can I achieve this when replaceStr is dynamically fetched?