1

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!

FED
  • 319
  • 1
  • 2
  • 12
  • Possible duplicate http://stackoverflow.com/questions/12895152/equivalent-of-innerhtml-for-text – bazeblackwood Aug 24 '14 at 13:15
  • No, it's not duplicated. i'm not interested in the text inside the inner tags. Check twice before you run to your "dislike" button. I'm requesting you to remove your false comment and dislike. – FED Aug 24 '14 at 13:20

1 Answers1

1

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.

Community
  • 1
  • 1
canon
  • 40,609
  • 10
  • 73
  • 97