JSFiddle - React, Tailwind, and code Playground

HTML

<div>
hi this is one line<br>
<p>This is second line</p>
<p>This 'word' is within quote</p>
<p>Another 'lorem ipsum' in quote</p>
</div>

CSS

.red{
color:red;
}

JavaScript

function replaceText(textToSearch,classToApply,searchAndWrapQuote)
{
    searchAndWrapQuote = searchAndWrapQuote || false;
    quote = (searchAndWrapQuote) ? "'" : "";
    
  $('div').html($('div').html().replace(new RegExp(quote+textToSearch+quote, 'g'),"<span class='"+classToApply+"'>"+quote+textToSearch+quote+"</span>"));
}

replaceText("lorem ipsum",'red',true)