I am trying to replace the font color of "bad" throughout an HTML page to red but there are two main issues, firstly it repeats the entire sentence before replacement and I do not want that and also the bad in "badly" was also replaced but I just want only the font in the word "bad" replaced. How do I go about this? Here is my code
window.onload = function() {
var text = document.getElementById("content");
var str = text.innerHTML,
reg = /red/ig;
var toStr = String(reg);
var color = (toStr.replace('\/g', '|')).substring(1);
var colors = color.replace("|");
if (colors.indexOf("red") > -1) {
str = str.replace(/bad/g, '<span style="color:red;">bad</span>');
}
document.getElementById("updated").innerHTML = str;
}
<div id="content">are they good or bad, tell me how badly it is if bad.</div>
<div id="updated"></div>