Function convertHTMLSpecialChars

  • Converts HTML special characters like &<>"'`’ to & escaped codes or vice versa. It handles named entities and hexadecimal numeric character references.

    Parameters

    • str: string

      The string to process.

    • unescape: boolean = true

      default=true - If true, converts & codes to characters. If false, converts characters to codes.

    Returns string

    The processed string.

    var normalHTML = convertHTMLSpecialChars('&lt;p&gt;This &amp; that &copy; 2023 '+
    '&quot;Quotes&quot;&#39;Apostrophes&#39; &euro;100 &#x263A;&lt;/p&gt;', true)
    console.log(normalHTML) // Returns: "<p>This & that © 2023 "Quotes" 'Apostrophes' €100 ☺</p>"