I want to convert the characters &, <, >, ", and ', to their corresponding HTML entities: &, <, >, ", and '. For example, "Tom & John" should become "Tom & John". How can I do this?
Asked
Active
Viewed 49 times
-1
George Stocker
- 56,270
- 29
- 173
- 234
Bernard Doci
- 667
- 1
- 14
- 22
1 Answers
1
Try this coding...
alert(HtmlSpecialConversion("Tom & John"))
function HtmlSpecialConversion(text) {
var map = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
return text.replace(/[&<>"']/g, function(m) { return map[m]; });
}
msvairam
- 834
- 5
- 12