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

这一生的挚爱 提交于 2019-11-28 06:03:51

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/

@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!

David Feldt

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>'));
});

This worked for me on my Drupal 8 website:

(function ($, Drupal) {
var regexp = /[\u2122]/;
$('body :not(script,sup)').contents().filter(function() {
    return this.nodeType === 3 && (regexp.test(this.nodeValue));
}).replaceWith(function() {
    return this.nodeValue.replace("\u2122", "<sup>&trade;</sup>");
});
})(jQuery, Drupal);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!