Strip HTML from Text JavaScript

前端 未结 30 3553
北荒
北荒 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: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 = '<p><div>This is </div>a <span>simple </span> <img src="test"></img>example.</p>';
    
    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.

    0 讨论(0)
  • 2020-11-21 05:35

    If you're running in a browser, then the easiest way is just to let the browser do it for you...

    function stripHtml(html)
    {
       let tmp = document.createElement("DIV");
       tmp.innerHTML = html;
       return tmp.textContent || tmp.innerText || "";
    }
    

    Note: as folks have noted in the comments, this is best avoided if you don't control the source of the HTML (for example, don't run this on anything that could've come from user input). For those scenarios, you can still let the browser do the work for you - see Saba's answer on using the now widely-available DOMParser.

    0 讨论(0)
  • 2020-11-21 05:36

    from CSS tricks:

    https://css-tricks.com/snippets/javascript/strip-html-tags-in-javascript/

    const originalString = `
      <div>
        <p>Hey that's <span>somthing</span></p>
      </div>
    `;
    
    const strippedString = originalString.replace(/(<([^>]+)>)/gi, "");
    
    console.log(strippedString);

    0 讨论(0)
  • 2020-11-21 05:36

    I think the easiest way is to just use Regular Expressions as someone mentioned above. Although there's no reason to use a bunch of them. Try:

    stringWithHTML = stringWithHTML.replace(/<\/?[a-z][a-z0-9]*[^<>]*>/ig, "");
    
    0 讨论(0)
  • 2020-11-21 05:36

    simple 2 line jquery to strip the html.

     var content = "<p>checking the html source&nbsp;</p><p>&nbsp;
      </p><p>with&nbsp;</p><p>all</p><p>the html&nbsp;</p><p>content</p>";
    
     var text = $(content).text();//It gets you the plain text
     console.log(text);//check the data in your console
    
     cj("#text_area_id").val(text);//set your content to text area using text_area_id
    
    0 讨论(0)
  • 2020-11-21 05:37

    Simplest way:

    jQuery(html).text();
    

    That retrieves all the text from a string of html.

    0 讨论(0)
提交回复
热议问题