javascript escape html
function escapeHtml(str) {
return str.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
}
4
8
function escapeHTML(text) {
var replacements= {"<": "&lt;", ">": "&gt;","&": "&amp;", """: "&quot;"};
return text.replace(/[<>&"]/g, function(character) {
return replacements[character];
});
}
Thank you!
8
0
3.5
2
function escapeHtml(text) {
return text
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
Thank you!
2
0
3.56
9
//escaping HTML with jquery<br/>var dangerousHTML = "<script>alert('Badabing Baby!');</script>";<br/>$("#myElementID").text(dangerousHTML); //.text() function will escape and display text<br/><br/><br/>//Alternatively, here is plain Javascript escape function<br/>function escapeHtml(str) {<br/> return str.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");<br/>}
Thank you!
9
0
Are there any code examples left?
New code examples in category Javascript