Strip HTML from Text JavaScript

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

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

30条回答
  •  梦如初夏
    2020-11-21 05:37

    If you want to keep the links and the structure of the content (h1, h2, etc) then you should check out TextVersionJS You can use it with any HTML, although it was created to convert an HTML email to plain text.

    The usage is very simple. For example in node.js:

    var createTextVersion = require("textversionjs");
    var yourHtml = "

    Your HTML

    • goes
    • here.
    "; var textVersion = createTextVersion(yourHtml);

    Or in the browser with pure js:

    
    
    

    It also works with require.js:

    define(["textversionjs"], function(createTextVersion) {
      var yourHtml = "

    Your HTML

    • goes
    • here.
    "; var textVersion = createTextVersion(yourHtml); });

提交回复
热议问题