Skip to content

Instantly share code, notes, and snippets.

@Saneyan
Last active December 20, 2015 14:29
Show Gist options
  • Select an option

  • Save Saneyan/6147023 to your computer and use it in GitHub Desktop.

Select an option

Save Saneyan/6147023 to your computer and use it in GitHub Desktop.
Escape HTML tags or script tag.
function escape(str) {
return str.replace(/[<>"\'=]/g, function (chr) {
return {
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#39;',
'=': '&#61;'
}[chr];
});
}
function escapeScript(str) {
return str.replace(/(?:<\s*script\s*(?:\s*(?:(?:[\w\-]+\s*=\s*["'].*["'])|(?:[\w\-]+))\s*)*>).*(?:<\s*\/\s*\s*script\s*>)/g, function (target) {
return escape(target);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment