Created
April 21, 2020 04:41
-
-
Save Elevista/30dfa4277950d3fee537937c416795c6 to your computer and use it in GitHub Desktop.
Decode HTML entities
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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