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.

2
  • 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. Commented May 18, 2017 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 I am not sure how to change it... :( Commented May 18, 2017 at 17:17

2 Answers 2

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");
}
Sign up to request clarification or add additional context in comments.

1 Comment

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~
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>

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.