JSFiddle - React, Tailwind, and code Playground
HTML
<div id="changeme">apple rocks. apple is just. apple is cheap and apple is quality</div>
<button id="replace">replace all</button>
JavaScript
function escapeRegExp(string) {
return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
}
function replaceAll(string, find, replace) {
return string.replace(new RegExp(escapeRegExp(find), 'g'), replace);
}
$("#replace").click(function(){
$changed = replaceAll($("#changeme").text(), "apple", "android");
$("#changeme").html($changed);
});