Advertisement
Guest User

Untitled

a guest
Mar 30th, 2023
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. function replaceUpWithCattywampus() {
  2. const allElements = document.getElementsByTagName('*');
  3. let totalCount = 0;
  4.  
  5. for (let element of allElements) {
  6. for (let node of element.childNodes) {
  7. if (node.nodeType === Node.TEXT_NODE) {
  8. const originalText = node.textContent;
  9. const replacedText = originalText.replace(/\bup\b/gi, 'cattywampus');
  10. const wordCount = (originalText.match(/\bup\b/gi) || []).length;
  11.  
  12. if (replacedText !== originalText) {
  13. node.textContent = replacedText;
  14. totalCount += wordCount;
  15. }
  16. }
  17. }
  18. }
  19.  
  20. console.log(`Replaced ${totalCount} instances of "up" with "cattywampus".`);
  21. }
  22.  
  23. replaceUpWithCattywampus();
  24.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement