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 53 times
-1

George Stocker
- 57,289
- 29
- 176
- 237

Bernard Doci
- 739
- 3
- 14
- 23
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
- 862
- 5
- 12