ChemPal Documentation - v1.7.0
    Preparing search index...

    Function decodeHTMLEntities

    • Decodes HTML entities in a string.

      Parameters

      • text: string

        The string to decode

      Returns string

      The decoded string

      decodeHTMLEntities("&lt;div&gt;Hello &amp; World&lt;/div&gt;") // Returns "<div>Hello & World</div>"
      decodeHTMLEntities("&#39;Hello&#39;") // Returns "'Hello'"
      export function decodeHTMLEntities(text: string): string {
      const entities: Record<string, string> = {
      '&nbsp;': ' ',
      '&lt;': '<',
      '&gt;': '>',
      '&amp;': '&',
      '&quot;': '"',
      '&#39;': "'",
      '&apos;': "'",
      '&cent;': '¢',
      '&pound;': '£',
      '&yen;': '¥',
      '&euro;': '€',
      '&copy;': '©',
      '&reg;': '®',
      } as const;

      return text
      .replace(/&[a-z]+;/gi, (match) => entities[match] || match)
      .replace(/&#(\d+);/gi, (match, dec) => String.fromCharCode(dec));
      }