Nodejs HTML Escape escapeHTML()

Here you can find the source of escapeHTML()

Method Source Code

var __entityMap = {
   "&": "&",
   "<": "&lt;",
   ">": "&gt;",
   '"': '&quot;',
   "'": '&#39;',
   "/": '&#x2F;'
};

String.prototype.escapeHTML = function() {
   return String(this).replace(/[&<>"'\/]/g, function(s) {
      return __entityMap[s];
   });// w  w  w .j av a 2s . c  o  m
}

String.prototype.unescapeHTML = function() {
   return String(this).replace("&amp;", "&").replace("&lt;", "<").replace("&gt;", ">").replace('&quot;', '"').replace('&#39;', "'").replace('&#x2F;', "/");
}

String.prototype.nl2br = function() {
   return String(this).replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1<br />$2');
}

String.prototype.capitalize = function() {
   return this.charAt(0).toUpperCase() + this.slice(1);
}

Number.prototype.pad = function(size) {
   var s = String(this);
   if (typeof(size) !== "number") {
      size = 2;
   }

   while (s.length < size) {
      s = "0" + s;
   }
   return s;
}

Related

  1. escapeHTML()
    String.prototype.escapeHTML = function() {
      return this.
        replace(/&/g, "&amp;").
        replace(/</g, "&lt;").
        replace(/>/g, "&gt;").
        replace(/\"/g, "&quot;");
    };
    console.log("<&>".escapeHTML()); 
    
  2. escapeHTML()
    String.prototype.escapeHTML = function(){
      var str = this.replace(/</g, '&lt;');
      return str.replace(/>/g, '&gt;');
    };
    
  3. escapeHTML()
    String.prototype.escapeHTML = function()
      var m = {"&": "&amp;", "<": "&lt;", ">": "&gt;", '"': '&quot;', "'": '&#39;', "/": '&#x2F;'};
      return String(this).replace(/[&<>"'\/]/g, function(s)
        return m[s];
      });
    
  4. escapeHTML()
    String.prototype.escapeHTML = function () {
      return(this.replace(/&/g,'&amp;').replace(/>/g,'&gt;').replace(/</g,'&lt;').replace(/"/g,'&quot;'));                                                 
    };
    
  5. escapeHTML()
    String.prototype.escapeHTML = function() {
      return this.replace(/</g,'&lt;').replace(/>/g, '&gt;');
    };
    
  6. escapeHTML()
    String.prototype.escapeHTML = function() {
      return this.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
    };
    
  7. escapeHTML()
    var __entityMap = {
      "&": "&amp;",
      "<": "&lt;",
      ">": "&gt;",
      '"': '&quot;',
      "'": '&#39;',
      "/": '&#x2F;'
    };
    String.prototype.escapeHTML = function() {
    ...
    
  8. escapeHTML()
    String.prototype.escapeHTML = function () {
        return ('' + this).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/"/g, '&quot;');
    };