Escape HTML by entity map - Node.js String

Node.js examples for String:HTML String

Description

Escape HTML by entity map

Demo Code


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

String.prototype.escapeHTML = function() {
  return String(this).replace(/[&<>"'\/]/g, function(s) {
    return __entityMap[s];
  });//  www .  ja v  a 2  s .  com
}

Related Tutorials