0

I'm trying to replace every instance of ® on every page with <sup>®</sup> but I can't seem to hit every single one. I currently have:

        $(document).ready(function(){
            var replaced = $('body').html().replace('®','<sup>®</sup>');
            $('body').html(replaced);
        });

but it's only replacing the first occurance of the ®. How can I get it to do all of them?

patricko
  • 831
  • 4
  • 16
  • 34
  • Use a regex to capture all instances: `replace(/®/g, '®');`. Also note that replacing content in this manner can lead to a FOUC. A much better solution would be to replace the content at the source, either in the HTML directly or in the database the content is being read from. – Rory McCrossan Jun 10 '21 at 15:32
  • @RoryMcCrossan i don't have the data coming from a db, but what do you mean in the HTML directly? – patricko Jun 10 '21 at 15:37
  • Open .html file, find/replace the character, save .html file :) – Rory McCrossan Jun 10 '21 at 16:56

1 Answers1

0

I think you should use replaceAll function

var replaced = $('body').html().replaceAll('®','<sup>®</sup>');

The difference between replaceAll() and replace():

If search argument is a string, replaceAll() replaces all occurrences of search with replaceWith , while replace() only the first occurence.

If search argument is a non-global regular expression, then replaceAll() throws a TypeError exception.