-1

I want to convert the characters &, <, >, ", and ', to their corresponding HTML entities: &amp;, &lt;, &gt;, &quot;, and &apos;. For example, "Tom & John" should become "Tom &amp; John". How can I do this?

George Stocker
  • 57,289
  • 29
  • 176
  • 237
Bernard Doci
  • 739
  • 3
  • 14
  • 23

1 Answers1

1

Try this coding...

alert(HtmlSpecialConversion("Tom & John"))


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

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