entities-decode
Version:
Fast function for decoding HTML entities.
17 lines (16 loc) • 475 B
JavaScript
/* IMPORT */
import entities from 'entities-standard';
/* HELPERS */
const entityRe = /&(?:([a-zA-Z0-9]+)|#([0-9]{1,6})|#[xX]([a-fA-F0-9]{1,6}));/g;
/* MAIN */
const decode = (str) => {
return str.replace(entityRe, (match, $1, $2, $3) => {
if ($1)
return entities[$1] || match;
if ($2)
return String.fromCodePoint(parseInt($2));
return String.fromCodePoint(parseInt($3, 16));
});
};
/* EXPORT */
export default decode;