Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding superscript <sup> tags around all trademark and registered trademark symbols

I am trying to add <sup></sup> tags around every ™, ®, © in my page.

I found this question: CSS superscript registration trademark which helped get me started.

The script works in the sense that the tags are being placed in the proper locations, but it is adding two <sup></sup> tags around each instead of just one.

Here is my JS adding the tags:

jQuery("body").html(
    jQuery("body").html().replace(/&reg;/gi, '<sup>&reg;</sup>').replace(/®/gi, '<sup>&reg;</sup>').
        replace(/&trade;/gi, '<sup>&trade;</sup>').
        replace(/™/gi, '<sup>&trade;</sup>').
        replace(/&copy;/gi, '<sup>&copy;</sup>').
        replace(/©/gi, '<sup>&copy;</sup>')
);

How can I make sure the tags are only added once per symbol? A conditional of some sort maybe?

like image 333
Ty Bailey Avatar asked Oct 14 '13 16:10

Ty Bailey


People also ask

Should the registered trademark symbol be superscript?

Proper Placement of the Registered Trademark Symbol It's recommended that the registered trademark symbol be used in connection with all registered trademarks. It should either be placed to the lower right (subscript) or to the upper right (superscript) of the actual mark.


3 Answers

This works for me.

$("p,h1,h2,h3,h4,li,a").each(function(){
    $(this).html($(this).html().replace(/&reg;/gi, '<sup>&reg;</sup>').replace(/®/gi, '<sup>&reg;   </sup>'));
});
like image 104
David Feldt Avatar answered Oct 19 '22 20:10

David Feldt


Instead of rewriting the entire markup (and removing all bound events), I'd go for something like that:

$('body :not(script)').contents().filter(function() {
    return this.nodeType === 3;
}).replaceWith(function() {
    return this.nodeValue.replace(/[™®©]/g, '<sup>$&</sup>');
});

DEMO: http://jsfiddle.net/QTfxC/

like image 31
VisioN Avatar answered Oct 19 '22 20:10

VisioN


@VisioN's answer didn't work that well for me, although it was facing the right direction. I tweaked it a little bit:

var regexp = /[\xAE]/;
$('body :not(script,sup)').contents().filter(function() {
    return this.nodeType === 3 && (regexp.test(this.nodeValue));
}).replaceWith(function() {
    return this.nodeValue.replace(regexp, '<sup>$&</sup>');
});

This method makes use of the character hex, instead of using the character code. I looked up the hex on character-codes.com and picked the character hex of the ® character. The value is AE so that's how I got to my solution. Let me know if this one worked for you!

like image 3
Timon de Groot Avatar answered Oct 19 '22 19:10

Timon de Groot