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.

convert unicode characters from HTML code

divyamodi
Mega Contributor

In one of my portal widget, I need to replace HTML Code of unicode characters with actual character to convert it to readable text. I'm unable to find a solution for this. Any ideas to achieve this?

1 ACCEPTED SOLUTION

AnveshKumar M
Tera Sage
Tera Sage

Hi @divyamodi 

To convert only Unicode HTML characters you can use the following script.

var html = "<p>Anvesh &#10008;</p>"
var text = html.replace(/&#(\d+);/g, function(match, dec) {
            return String.fromCharCode(dec);
        })
        .trim();

 

To convert all HTML to Text you can use the following code.

 

var html = "<p>Anvesh &#10008;</p>"
var text = html.replace(/<\/?p>/gi, "\n")
        .replace(/<\/?br\s*\/?>/gi, "\n")
        .replace(/<li>/gi, " * ")
        .replace(/(<(?:.|\n)*?>)/gm, "")
        .replace(/&nbsp/g, " ")
        .replace(/(\n\s*)+/g, "\n")
		.replace(/&#(\d+);/g, function(match, dec) {
            return String.fromCharCode(dec); //Returns the special character from the decimal code representation and returns the entire decoded string.
        })
        .trim();

 

I found this code in an OOTB Script Include.

 

Please mark my answer helpful and accept as solution if it helped you 👍✔️

Thanks,
Anvesh

View solution in original post

4 REPLIES 4

Abbas_5
Tera Sage
Tera Sage

Hello @divyamodi ,
Please refer to the below link:
https://www.servicenow.com/community/developer-forum/to-convert-html-character-to-plain-text/m-p/189...

 

If it is helpful, mark it as a thumbs-up and accept the correct solution.
Thanks & Regards,
Abbas

Thanks Abbas for the quick help.

AnveshKumar M
Tera Sage
Tera Sage

Hi @divyamodi 

To convert only Unicode HTML characters you can use the following script.

var html = "<p>Anvesh &#10008;</p>"
var text = html.replace(/&#(\d+);/g, function(match, dec) {
            return String.fromCharCode(dec);
        })
        .trim();

 

To convert all HTML to Text you can use the following code.

 

var html = "<p>Anvesh &#10008;</p>"
var text = html.replace(/<\/?p>/gi, "\n")
        .replace(/<\/?br\s*\/?>/gi, "\n")
        .replace(/<li>/gi, " * ")
        .replace(/(<(?:.|\n)*?>)/gm, "")
        .replace(/&nbsp/g, " ")
        .replace(/(\n\s*)+/g, "\n")
		.replace(/&#(\d+);/g, function(match, dec) {
            return String.fromCharCode(dec); //Returns the special character from the decimal code representation and returns the entire decoded string.
        })
        .trim();

 

I found this code in an OOTB Script Include.

 

Please mark my answer helpful and accept as solution if it helped you 👍✔️

Thanks,
Anvesh

divyamodi
Mega Contributor

Thanks for providing the script. It's really helpful.