escape in html string code example
Example 1: html escape characters
Escapes or unescapes an HTML file removing traces of offending characters that could be wrongfully interpreted as markup.
The following characters are reserved in HTML and must be replaced with their corresponding HTML entities:
" is replaced with "
& is replaced with &
< is replaced with <
> is replaced with >
Example 2: jquery escape html string
var dangerousHTML = "<script>alert('Badabing Baby!');</script>";
$("#myElementID").text(dangerousHTML);
function escapeHtml(str) {
return str.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
}