Top Banner

Saturday, July 25, 2015

Javascript equivalent of PHP htmlspecialchars() function

Javascript version of PHP htmlspecialchars() function

function escapeHtml(text) {
  var map = {
    '&': '&',
    '<': '&lt;',
    '>': '&gt;',
    '"': '&quot;',
    "'": '&#039;'
  };

  return text.replace(/[&<>"']/g, function(m) { return map[m]; });
}

No comments:

Post a Comment