0

This is a follow-up of the thread at Replace text in a website.

The top answer given by @Paulpro on replacing texts in a website works like a charm, but I do not know how to use regular expressions in the last line:

replaceTextOnPage('original text', 'new text').

I tried to use something like

replaceTextOnPage(/(?!bingogame)bingo/g, 'bridge')

to replace the text 'bingo' with 'bridge' (excluding matches found in 'bingogame') but the whole script was messed up by that:

function replaceTextOnPage(from, to){
    getAllTextNodes().forEach(function(node){
       node.nodeValue = node.nodeValue.replace(new RegExp(quote(from), 'g'), to); 
    });

    function getAllTextNodes(){
        var result = [];

        (function scanSubTree(node){
            if(node.childNodes.length) 
                for(var i = 0; i < node.childNodes.length; i++) 
                    scanSubTree(node.childNodes[i]);
            else if(node.nodeType == Node.TEXT_NODE) 
                result.push(node);
        })(document);

        return result;
    }

    function quote(str){
        return (str+'').replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
    }
}

replaceTextOnPage(/(?!bingogame)bingo/g, 'bridge')

Can someone tell me how to incorporate regular expression correctly? Pardon my stupidity; grandma talking here.

Community
  • 1
  • 1
Kelvin C
  • 27
  • 6
  • The quote function expects a string, but you're passing in a regex literal. In the answer to your original question the two params passed into replaceTextOnPage are both strings. That might not cause an error but I would triple check that the result of quote(regex literal) is the pattern you expect. – James May 18 '17 at 16:59
  • The link shows that how the script works when regular expressions are not used and how it just fails in the case when I include the regular expressions. [link](https://jsfiddle.net/pj12j0um/) I am not sure how to change it... :( – Kelvin C May 18 '17 at 17:17

2 Answers2

1

I think you want to skip calling the quote function if you are passing in a regular expression literal (and allow it to run if you are passing in a string).

Give this a try:

function quote(str){
  // don't do anything if the parameter is an object (regex literal)
  if (typeof str == 'object') return str;

  return (str+'').replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
}
James
  • 20,957
  • 5
  • 26
  • 41
  • Oh My.... It works! Not sure how because I am practically brain-dead when it comes to computer~ but thanks a lot, James! :) :) Contribution again~ – Kelvin C May 18 '17 at 17:36
0

you should put innerText

var text = document.getElementsByTagName("p")[0];
//match only "bingo"
text.innerText = text.innerText.replace(/\bbingo\b/gi, "bridge")
<p>bingoo bingo bbingo bingogame bingo bingo game bing bingoggame</p>
alessandrio
  • 4,282
  • 2
  • 29
  • 40
  • Thank you for your answer! This method works, but i forgot to mention that the website uses javascript, and unfortunately the above method will mess up the code of the website. That is why the method given by @Paulpro is needed. – Kelvin C May 18 '17 at 17:25