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): string {
const entities: Record<string, string> = {
' ': ' ',
'<': '<',
'>': '>',
'&': '&',
'"': '"',
''': "'",
''': "'",
'¢': '¢',
'£': '£',
'¥': '¥',
'€': '€',
'©': '©',
'®': '®',
} 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.