Skip to content

Instantly share code, notes, and snippets.

@Elevista
Created April 21, 2020 04:41
Show Gist options
  • Save Elevista/30dfa4277950d3fee537937c416795c6 to your computer and use it in GitHub Desktop.
Save Elevista/30dfa4277950d3fee537937c416795c6 to your computer and use it in GitHub Desktop.
Decode HTML entities
const entities = {
lt: '<',
gt: '>',
amp: '&',
quot: '"',
apos: `'`,
cent: '¢',
pound: '£',
yen: '¥',
euro: '€',
copy: '©',
reg: '®',
nbsp: ' '
}
const regex = /&(?:([a-z]+)|#(\d+));/g
const fromCharCode = code => code && String.fromCharCode(code)
const replace = (match, entityName, charCode) => entities[entityName] || fromCharCode(charCode) || match
export const decodeEntities = str => `${str}`.replace(regex, replace)
export default decodeEntities
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment