I'm trying to use javascript to replace characters in a paragraph with styled characters. If I replace one character with one style it works fine, but when I try to replace 3 different characters with 3 different styles the paragraph is printed 3 times, each with only 1 style change. I want the 3 styles to take effect in 1 paragraph. Below is the code I am using. Thank you.
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction (){
var pText=document.getElementById("alteredText").innerHTML;
var eSpan=pText.replace(/e/g,"<span style='position:relative;color:red;top:.05em;'>e</span>");
var tSpan=pText.replace(/t/g,"<span style='position:relative;color:blue;top:-.05em;'>t</span>");
var sSpan=pText.replace(/s/g,"<span style='color:green;'>s</span>");
var n = eSpan.concat(tSpan,sSpan);
document.getElementById("alteredText").innerHTML=n;
}
</script>
</head>
<body onLoad="myFunction()">
<p id="alteredText">
The quick brown fox jumped over the lazy sleeping dog.
The slow shiny robot chased the quick brown fox.
The lazy sleeping dog awoke and barked at the slow shiny robot.
</p>
</body>
</html>
pText.replace().replace().replace()), however that wont work, as the newly added text will match ontands, so you need to use a better regex and more functional replacement that checks what character was matched and replaces it appropriately.