1function escapeHtml(str) {
2 return str.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
3}
1function escapeHtml(text) {
2 return text
3 .replace(/&/g, "&")
4 .replace(/</g, "<")
5 .replace(/>/g, ">")
6 .replace(/"/g, """)
7 .replace(/'/g, "'");
8}
9
1function escapeHTML(text) {
2 var replacements= {"<": "<", ">": ">","&": "&", """: """};
3 return text.replace(/[<>&"]/g, function(character) {
4 return replacements[character];
5 });
6}
1//escaping HTML with jquery
2var dangerousHTML = "<script>alert('Badabing Baby!');</script>";
3$("#myElementID").text(dangerousHTML); //.text() function will escape and display text
4
5
6//Alternatively, here is plain Javascript escape function
7function escapeHtml(str) {
8 return str.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
9}