Skip to content

Instantly share code, notes, and snippets.

@jamescostian
Last active March 29, 2016 14:30
Show Gist options
  • Save jamescostian/34a1e82d5f23c42a87ce to your computer and use it in GitHub Desktop.
Save jamescostian/34a1e82d5f23c42a87ce to your computer and use it in GitHub Desktop.
This is like PHP's htmlentities, but for JS
const htmlEntities = (str) => String(str)
.replace(/&/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&apos;')
const unhtmlEntities = (str) => String(str)
.replace(/&amp;/g, '&')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&quot;/g, '"')
.replace(/&apos;/g, "'")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment