Skip to content

Instantly share code, notes, and snippets.

@xuanfeng
Last active August 29, 2015 14:09
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 xuanfeng/fc00cccec9bf89b1d34a to your computer and use it in GitHub Desktop.
Save xuanfeng/fc00cccec9bf89b1d34a to your computer and use it in GitHub Desktop.
html转码
function htmlDecode(str){
return str
.replace(/'/g, '\'')
.replace(/<br\s*(\/)?\s*>/g, '\n')
.replace(/&nbsp;/g, ' ')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&quot;/g, '"')
.replace(/&amp;/g, '&');
}
function htmlEncode(str){
return str
.replace(/'/g, '&#39;')
.replace(/\n/g, '<br*>')
.replace(/\s/g, '&nbsp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/&/g, '&amp;');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment