Skip to content

Instantly share code, notes, and snippets.

@xu33
Created September 18, 2013 02:51
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 xu33/6603865 to your computer and use it in GitHub Desktop.
Save xu33/6603865 to your computer and use it in GitHub Desktop.
html encode and decode
function htmlEscape(str) {
return String(str)
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;');
}
function htmlUnescape(value){
return String(value)
.replace(/&quot;/g, '"')
.replace(/&#39;/g, "'")
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&amp;/g, '&');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment