Process illegal characters in XML

ampersand (&) is escaped to &

double quotes (") are escaped to "

single quotes (') are escaped to ' 

less than (<) is escaped to &lt; 

greater than (>) is escaped to &gt;

How to apply this with suitescript : 

        /**
        * Replace illegal characters with their HTML entities
        * @param {string} str - The string to replace special characters in
        * @returns {string} - The modified string with HTML entities
        */
        function replaceIllegalCharacters(str) {
            str = str.replace(/&/g, '&amp;');
            str = str.replace(/"/g, '&quot;');
            str = str.replace(/'/g, '&apos;');
            str = str.replace(/</g, '&lt;');
            str = str.replace(/>/g, '&gt;');

            return str;
        }

Leave a comment

Your email address will not be published. Required fields are marked *