Skip to content

Instantly share code, notes, and snippets.

@leodutra
Last active December 15, 2015 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leodutra/5215180 to your computer and use it in GitHub Desktop.
Save leodutra/5215180 to your computer and use it in GitHub Desktop.
Encodes (escapes) HTML Special Chars
function htmlSpecialCharsEntityEncode(str) {
return str.replace(/[<>&\r\n"']/gm, function (match) {
return '&' + {
'<': 'lt;',
'>': 'gt;',
'&': 'amp;',
'\r': "#13;",
'\n': "#10;",
'"': 'quot;',
"'": 'apos;' /*single quotes just to be safe*/
}[match];
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment