1

I want to replace some words of the document without changing any html tags or js codes.

Basically what I do is;

document.body.innerHTML = document.body.innerHTML.replace('lorem','new lorem');

But this code will replace any 'lorem'. I want to avoid tags like; <script... var lorem = 123; <div class="lorem", <a id="lorem" etc.

How can I do this in JS?

2 Answers 2

5

Walk the DOM, and .replace() the values of text nodes.

function walk(el, fn) {
    for (var i = 0, len = el.childNodes.length; i < len; i++) {
        var node = el.childNodes[i];
        if (node.nodeType === 3)
            fn(node);
        else if (node.nodeType === 1 && node.nodeName !== "SCRIPT")
            walk(node, fn);
    }
}

walk(document.body, function(node) {
    var text = node.data.split("foo"),
        parent = node.parentNode,
        i = 1,
        newNode;

    parent.insertBefore(textNode(text[0]), node);

    for (; i < text.length; i += 2) {
        (newNode = document.createElement("b"))
                           .appendChild(textNode("bar"));
        parent.insertBefore(newNode, node);
        parent.insertBefore(textNode(text[i]), node);
    }
    parent.removeChild(node);
});

function textNode(txt) {
    return document.createTextNode(txt);
}
Sign up to request clarification or add additional context in comments.

9 Comments

Thank you. It works great. There is only one thing, I can't work html tags with replace. I want to do replace(/foo/g, "<b>bar</b>"); but it doesn't affect because of node.data
@kentilyuk: I thought you didn't want to replace any elements. The DOM is not a string, so no, that won't work. What do you actually need to do?
I want to find 'foo' and change it with or without html tags like 'bar', '<b>bar</b>', '<span class="cname">bar</span>', '<a href="#">bar</a>' etc.
@kentilyuk: So you're saying you want to wrap the matched text in an element? Again, the DOM is not a string. We won't be using HTML markup to accomplish this.
Yes. That's what I try to do.
|
0

Another way of doing this is using regular expressions, science you can't insert HTML tags to a text node.

First I am using a regular expressions to catch parts of the body.innerHTML property that are outside of the HTML tags.

But, the string contains also another data, and if I will directly replace it with thge new data some text will be lost.

Insted, I get each match as an argument for a function, and inside this function I use another regular expression.

Notice that you have to replace "foo" twice.

The code:

document.body.innerHTML=document.body.innerHTML.replace(/<[^(script)][^>]*>[^<]*(foo)[^<]*</g,function(match){ return match.replace(/foo/,"newWord"); });

1 Comment

This code changes not only text but also js codes. I have <script tag in body. And it turned like this; <script type="text/javascript"> var <b>bar</b> = '';

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.