How to extract text from Link in Javascript?

后端 未结 4 561
鱼传尺愫
鱼传尺愫 2021-01-27 14:08

I have following string:

StackOverFlow

How can I extract text (i.e. StackOver

4条回答
  •  灰色年华
    2021-01-27 14:39

    No regex required:

    var t_ = document.createElement('div'),
        a;
    t_.innerHTML = htmlString; // <- string containing your HTML
    a = t_.children[0];
    
    var text = a.textContent || a.innerText; // W3C vs IE
    

    Actually parsing HTML with regular expressions is evil. Although it might be easy to come up with an expression for your specific case, it might not work well for a different string.

提交回复
热议问题