Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Special characters not appearing in the right way (Regex)

F_bio Santos
Kilo Sage

Hi, Im having a problem in the "activity stream", we are using a HTML field to send messages through the "activity stream" the problem is that when we use "special characters" one after another (example: # @ « » º ª , " ' § $ % _ - .) the .replace() is not working instead of getting the "special character" we get somethink like (example: &trg) which is what we are replacing for the real "special character". I was able to identified at least 3 that the .replace() is not working not sure why, which are this one: "« º ª".

 

Code that we are using for each one:

 

 

.replace(/«/g, '«')

.replace(/&ordf/g, 'ª')

.replace(/&ordm/g, 'º')

 

 

Code of the function that we are using for the special characters:

   

 

 function decodeHTML(str) {

        var a = str.replace(/<(?:.|\n)*?>/gm, ''); //Remove tags

        var c = a.replace(/&gt;/g, '>').replace(/&lt;/g, '<').replace(/&amp;/g, '&').replace(/%23%20%20/g, '#').replace(/%24/g, '$').replace(/%40/g, '@').replace(/%25/g, '%').replace(/%2F/g, '/').replace(/%28/g, '(').replace(/%29/g, ')').replace(/%3D/g, '=').replace(/%3F/g, '?').replace(/%5B/g, '[').replace(/%5D/g, ']').replace(/%5D/g, ']').replace(/%7B/g, '{').replace(/%7D/g, '}').replace(/%7D/g, '}').replace(/&laquo;/g, '«').replace(/&raquo;/g, '»').replace(/%2B/g, '+').replace(/%2D/g, '-').replace(/%2C/g, ',').replace(/%2E/g, '.').replace(/%3B/g, ';').replace(/%3A/g, ':').replace(/%5F/g, '_').replace(/&ordf/g, 'ª').replace(/&ordm/g, 'º').replace(/%C3%A7/g, 'ç').replace(/%C2%A7/g, '§').replace(/%2A/g, '*').replace(/%5E/g, '^').replace(/%7C/g, '|').replace(/%21/g, '!').replace(/%22/g, '"').replace(/%C2%A3/g, '£').replace(/%C3%A0/g, 'à').replace(/%C3%A3/g, 'ã').replace(/%60/g, '`').replace(/%7E/g, '~').replace(/%C2%B4/g, '´').replace(/&nbsp;/g, "s").replace(/&nbsp; &nbsp;/g, "s");

         return c.replace(/&#(\d+);/g, function(match, dec) {

            return String.fromCharCode(dec);

        });

    }

 

2 REPLIES 2

Maik Skoddow
Tera Patron
Tera Patron

Hi @F_bio Santos 

please use 

new GlideSPScriptable().stripHTML(yourHtmlStr);

Maik

Aniket Chavan
Tera Sage
Tera Sage

Hello @F_bio Santos 
 
I have not tested but you can try the below code and let me know if you still facing the issues or not

function decodeHTML(str) {
    var a = str.replace(/<(?:.|\n)*?>/gm, ''); // Remove tags
    var c = a.replace(/&gt;/g, '>').replace(/&lt;/g, '<').replace(/&amp;/g, '&').replace(/%23%20%20/g, '#').replace(/%24/g, '$').replace(/%40/g, '@').replace(/%25/g, '%').replace(/%2F/g, '/').replace(/%28/g, '(').replace(/%29/g, ')').replace(/%3D/g, '=').replace(/%3F/g, '?').replace(/%5B/g, '[').replace(/%5D/g, ']').replace(/%5D/g, ']').replace(/%7B/g, '{').replace(/%7D/g, '}').replace(/%7D/g, '}').replace(/\u00AB/g, '«').replace(/\u00BB/g, '»').replace(/%2B/g, '+').replace(/%2D/g, '-').replace(/%2C/g, ',').replace(/%2E/g, '.').replace(/%3B/g, ';').replace(/%3A/g, ':').replace(/%5F/g, '_').replace(/\u00AA/g, 'ª').replace(/\u00BA/g, 'º').replace(/%C3%A7/g, 'ç').replace(/%C2%A7/g, '§').replace(/%2A/g, '*').replace(/%5E/g, '^').replace(/%7C/g, '|').replace(/%21/g, '!').replace(/%22/g, '"').replace(/%C2%A3/g, '£').replace(/%C3%A0/g, 'à').replace(/%C3%A3/g, 'ã').replace(/%60/g, '`').replace(/%7E/g, '~').replace(/%C2%B4/g, '´').replace(/ /g, "s").replace(/   /g, "s");

    return c.replace(/&#(\d+);/g, function(match, dec) {
        return String.fromCharCode(dec);
    });
}

 

Please Mark Correct if this solves your issue and also mark 👍Helpful if you find my response worthy based on the impact.

 

Regards,
Aniket