Nodejs HTML Strip stripHTML()

Here you can find the source of stripHTML()

Method Source Code

/**/* w  w  w.  j  av  a  2  s.  c o m*/
@Name: String.prototype.stripHTML
@Author: Paul Visco
@Version: 1.0 11/19/07
@Description: Removes all HTML tags from a string
@Return: String The original string without any HTML markup
@Example:
var myString = 'hello <p>world</p> on earth';

var newString = myString.stripHTML();
//newString = 'hello world on earth'
*/
String.prototype.stripHTML = function(){
   var re = new RegExp("(<([^>]+)>)", "ig");
   var str = this.replace(re, "");
   var amps = ["&nbsp;", "&amp;", "&quot;"];
   var replaceAmps =[" ", "&", '"'];
   for(var x=0;x<amps.length;x++){
      str = str.replace(amps[x], replaceAmps[x]);
   }
   
   re = new RegExp("(&(.*?);)", "ig");
   str = str.replace(re, "");
   
   return str;
};

Related

  1. stripHTML()
    String.prototype.stripHTML = function(){
      return this.replace(/<(?:.|\s)*?>/g, "");
    };
    
  2. stripHTML()
    String.prototype.stripHTML = function(){
        return $('<p/>').html(this.toString()).text();
    
  3. stripHTMLEntities()
    String.prototype.stripHTMLEntities = function() {
      var el = document.createElement("div");
      var html = this.replace(/<img/g, '<x-img');
      el.innerHTML = html;
      return el.innerText;
    
  4. stripHtml()
    String.prototype.stripHtml = function() {
       var tmp = document.createElement("DIV");
       tmp.innerHTML = this;
       return tmp.textContent || tmp.innerText || "";
    };
    
  5. stripHtml()
    String.prototype.stripHtml = function () {
        var _self = this.replace(/(<([^>]+)>)/ig, '');
        return _self;
    
  6. stripHtml()
    String.prototype.stripHtml = function() {
      return this.replace(/<[^>]+>/g, "");
    };
    console.assert("<p>Shoplifters of the World <em>Unite</em>!</p>".stripHtml() == "Shoplifters of the World Unite!");
    console.assert("1 &lt; 2".stripHtml() == "1 &lt; 2");