FED
FED

Reputation: 319

Remove a part of a string which inside tagnames (via js)

I have this node: <p>text.. <span>hi</span><a>bye</a> more text..</p>

And i'd like to get the element content with the direct text only, without any tags (a and span, in the example above) inside, as if I got: <p>text.. more text..</p>.

Can you please show me the way to do it via specific regex?

Thanks a lot!

Upvotes: 1

Views: 62

Answers (1)

canon
canon

Reputation: 41665

I'd suggest you simply remove any ChildNode having a nodeType other than 3 (TEXT_NODE). (fiddle):

(function(){
    var p = document.querySelector("p");
    var ln = p.childNodes.length;
    while (ln--){
        if(p.childNodes[ln].nodeType !== 3) { // not a text node   
            p.removeChild(p.childNodes[ln]);
        }
    }    
})();

Which leaves you with this:

<p>text..  more text..</p>

Alternatively, you could build up a string from the text nodes and set p.textContent (fiddle), i.e.:

(function () {
    var p = document.querySelector("p");
    p.textContent = [].reduce.call(p.childNodes, function (p, c) {
        c.nodeType === 3 && p.push(c.data);
        return p;
    }, []).join("");
})();

See also Node.childNodes and Node.removeChild().

Note: Please don't use regex to parse html. See this answer.

Upvotes: 1

Related Questions