Strip HTML from Text JavaScript

前端 未结 30 3706
北荒
北荒 2020-11-21 05:08

Is there an easy way to take a string of html in JavaScript and strip out the html?

30条回答
  •  Happy的楠姐
    2020-11-21 05:34

    It is also possible to use the fantastic htmlparser2 pure JS HTML parser. Here is a working demo:

    var htmlparser = require('htmlparser2');
    
    var body = '

    This is
    a simple example.

    '; var result = []; var parser = new htmlparser.Parser({ ontext: function(text){ result.push(text); } }, {decodeEntities: true}); parser.write(body); parser.end(); result.join('');

    The output will be This is a simple example.

    See it in action here: https://tonicdev.com/jfahrenkrug/extract-text-from-html

    This works in both node and the browser if you pack you web application using a tool like webpack.

提交回复
热议问题