-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?

0

1 Answer 1

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]; });
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.