JSFiddle - React, Tailwind, and code Playground

by M Shameer

HTML

<p id="old-text">This is a bunch of text that will be searched through and matches will be bolded</p>
<p id="new-text"></p>

JavaScript

//function accepts a string to look through and a string to look for
function boldString(str, find){
    var re = new RegExp(find, 'g');
    return str.replace(re, '<b>'+find+'</b>');
}

//this is just pulling the test HTML from above
var oldText = document.getElementById("old-text").innerHTML;

//calls boldString() function and passes in the old-text content and looks for the string "ch"
var result = boldString(oldText, "ch");

//this is just for this example, but updates the new-text element with the result of the function
document.getElementById("new-text").innerHTML = result;