jQuery find text and replace with custom text

(function($){ $(document).ready(function(){ $("div").each(function () { // var this is the current element's DOM object if (this.innerHTML.indexOf("CōV") != -1) { //$(this).addClass('cov'); $(this).html( $(this).html().replace(/(\")?(CōV)(?!\")/gi, function ($0, $1) { return $1 ? $0 : '<span class="cov">CōV</span>'; }) ); } }); }); }(jQuery));
found and oldie snippet and figured it should be added to the collection

1 Response

Shorter alternative:



https://codepad.co/snippet/GoJAE4np

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.