Special characters not appearing in the right way (Regex)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2023 02:21 AM
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(/ª/g, 'ª')
.replace(/º/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(/>/g, '>').replace(/</g, '<').replace(/&/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(/«/g, '«').replace(/»/g, '»').replace(/%2B/g, '+').replace(/%2D/g, '-').replace(/%2C/g, ',').replace(/%2E/g, '.').replace(/%3B/g, ';').replace(/%3A/g, ':').replace(/%5F/g, '_').replace(/ª/g, 'ª').replace(/º/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);
});
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2023 03:55 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2023 04:05 AM
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(/>/g, '>').replace(/</g, '<').replace(/&/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