ChemPal Documentation - v0.0.13-beta.5
    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) {
      const entities: Record<string, string> = {
      /* eslint-disable */
      "&nbsp;": " ",
      "&lt;": "<",
      "&gt;": ">",
      "&amp;": "&",
      "&quot;": '"',
      "&#39;": "'",
      "&apos;": "'",
      "&cent;": "¢",
      "&pound;": "£",
      "&yen;": "¥",
      "&euro;": "€",
      "&copy;": "©",
      "&reg;": "®",
      /* eslint-enable */
      } as const;

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