Alex Dai Playground

[javascript] htmlspecialchar

| Comments

var _htmlspecialchars_decode = function (html) {
    var txt = document.createElement("textarea");
    txt.innerHTML = html;
    return txt.value;
};

var _htmlspecialchars_encode = function (text) {
  var map = {
    '&': '&',
    '<': '&lt;',
    '>': '&gt;',
    '"': '&quot;',
    "'": '&#039;'
  };
  return text.replace(/[&<>"']/g, function(m) { return map[m]; });
};

來源: http://stackoverflow.com/questions/1787322/htmlspecialchars-equivalent-in-javascript

Comments

comments powered by Disqus