JQuery or CSS: Replacing Label ® with <sup> & reg; </sup>
I have this HTML header:
<h1>Time Matters®</h1> (equals to: Time Matters®) I need a way to manage the label & reg; "either using CSS or using jQuery. As far as I know, I don’t think it’s possible to customize" & reg; "using CSS, is there?
Now, if jQuery is used, I need to conclude ' & reg; 'in <sup> for example:
<h1>Time Matters<sup>®</sup></h1> ... so I can target the element through CSS.
I tried to adapt this tutorial , but the code encapsulates all the words in the <span>.
Any idea how to do this? Any help would be greatly appreciated.
Thanks.
+4
3 answers
Normal Javascript String .replace() should do this:
$(function() { $('h1').each(function(i, elem) { $(elem).html(function(i, html) { return html.replace(/(®)/, "<sup>$1</sup>"); }); }); }); Example: http://www.jsfiddle.net/4yUqL/24/
// off-topic I just figured it out, wow it looks pretty impressive
(®)(®) +8
Here, the Javascript solution:
var replaceHTMLCharacterEntities = function (expression, tag, superscript) { var regex = new RegExp(expression, 'g'), iTag = tag.toUpperCase(), iSuperscript = expression[superscript](), tags = document.getElementsByTagName(iTag), i = 0, tagsLength = tags.length, html = ""; for (i; i < tagsLength; i++) { html = tags[i].innerHTML.replace(regex, iSuperscript); tags[i].innerHTML = html; } }; replaceHTMLCharacterEntities("\u00a9", "h1", "sup"); replaceHTMLCharacterEntities("\u00AE", "p", "sub"); replaceHTMLCharacterEntities("\u24C8", "p", "sub"); 0