The string to decode
The decoded string
decodeHTMLEntities("<div>Hello & World</div>") // Returns "<div>Hello & World</div>"
decodeHTMLEntities("'Hello'") // Returns "'Hello'"
export function decodeHTMLEntities(text: string) {
const entities: Record<string, string> = {
/* eslint-disable */
" ": " ",
"<": "<",
">": ">",
"&": "&",
""": '"',
"'": "'",
"'": "'",
"¢": "¢",
"£": "£",
"¥": "¥",
"€": "€",
"©": "©",
"®": "®",
/* eslint-enable */
} as const;
return text
.replace(/&[a-z]+;/gi, (match) => entities[match] || match)
.replace(/&#(\d+);/gi, (match, dec) => String.fromCharCode(dec));
}
Decodes HTML entities in a string.