Skip to content

Instantly share code, notes, and snippets.

@arnehormann
Last active October 11, 2015 09:38
Show Gist options
  • Save arnehormann/3839134 to your computer and use it in GitHub Desktop.
Save arnehormann/3839134 to your computer and use it in GitHub Desktop.
escape text for inclusion by innerHTML as one handy fully namespaced function
var escapeHTML = (function() {
var regexp = /[<>&'"\/]/g
, replacements =
{ '&': '&amp;'
, '<': '&lt;'
, '>': '&gt;'
, '/': '&#x2F;'
, '"': '&quot;'
, "'": '&#39;'
}
, replacer = function(match) {
return replacements[match]
}
return function(string) {
return String(string).replace(regexp, replacer)
}
})()
@arnehormann
Copy link
Author

Have a look at http://js-quasis-libraries-and-repl.googlecode.com/svn/trunk/safetemplate.html first. All simple approaches for HTML-escaping come with a lot of problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment