Skip to content

Instantly share code, notes, and snippets.

@jonvuri
Last active December 15, 2015 04:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonvuri/5204669 to your computer and use it in GitHub Desktop.
Save jonvuri/5204669 to your computer and use it in GitHub Desktop.
A function to HTML encode a string
function htmlEncode(str) {
return String(str)
.replace(/&/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#39;')
.replace(/\//g, '&#47;');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment