Created
February 4, 2026 00:35
-
-
Save cmbaughman/620b7d44034562fde33e6dced7d1ea71 to your computer and use it in GitHub Desktop.
Replace ® with superscript version
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| window.addEventListener('DOMContentLoaded', () => { | |
| const walk = document.createTreeWalker( | |
| document.body, | |
| NodeFilter.SHOW_TEXT, | |
| null, | |
| false | |
| ); | |
| const reg = /®/g; | |
| let node; | |
| // Use a list to avoid mutating the DOM while walking | |
| const nodesToReplace = []; | |
| while (node = walk.nextNode()) { | |
| if (node.parentElement.tagName !== 'SUP' && node.nodeValue.includes('®')) { | |
| nodesToReplace.push(node); | |
| } | |
| } | |
| nodesToReplace.forEach(textNode => { | |
| const parent = textNode.parentElement; | |
| const parts = textNode.nodeValue.split(reg); | |
| const fragment = document.createDocumentFragment(); | |
| parts.forEach((part, i) => { | |
| fragment.appendChild(document.createTextNode(part)); | |
| if (i < parts.length - 1) { | |
| const sup = document.createElement('sup'); | |
| sup.textContent = '®'; | |
| fragment.appendChild(sup); | |
| } | |
| }); | |
| parent.replaceChild(fragment, textNode); | |
| }); | |
| }); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Alternative version: