Javascript: convert HTML special characters








HTML special characters

  • & (ampersand) becomes &amp
  • " (double quote) becomes &quot when ENT_NOQUOTES is not set.
  • ' (single quote) becomes '&#039 only when ENT_QUOTES is set.
  • < (less than) becomes &lt
  • > (greater than) becomes &gt
  • # (hash) becomes &#035


Javascript function to convert HTML special characters

function converHtml(s) {
  var e = document.createElement("div");
  e.innerText = e.textContent = s;
  return e.innerHTML.replace(/"/g, "&quot;").replace(/#/,'&#035').replace(/'/,'&#039');
}

See also




No comments:

Post a Comment