Showing Special characters in a string field

Pavan Dev
Tera Contributor

Hi,

 

I'm trying to display the text with special characters from the HTML field in the string field. However, the text is shown with the ASCII characters instead of actual special characters. See the example below

 

Text in HTML field: Test's

Text in String field: Test's

Code: current.test.replace(/<.*?>/g, '').trim();

Am I missing anything here?

 

Thanks,

Pavan.

1 ACCEPTED SOLUTION

Hi @Pavan Dev 

Try this 

var htmlText = current.u_html_field;

var decodedText = htmlText
    .replace(/&#(\d+);/g, function(match, dec) {
        return String.fromCharCode(dec);
    })
    .replace(/&lt;/g, '<')
    .replace(/&gt;/g, '>')
    .replace(/&amp;/g, '&')
    .replace(/&quot;/g, '"')
    .replace(/&apos;/g, "'")
    // Add more replacements for other named entities if needed
    .replace(/<[^>]*>/g, '')
    .trim();

gs.print(decodedText);

 

OR

 

// Get the HTML text from the field
var htmlText = current.u_html_field;

// Decode HTML entities
var decodedText = GlideStringUtil.format(htmlText);

// Return the decoded text
return decodedText;

 

🙂

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)

View solution in original post

6 REPLIES 6

-O-
Kilo Patron
Kilo Patron

If this is indeed server side script, you can use

new global.GlideSPScriptable().stripHTML('Test&#39;s');

to get the plain text variant of an HTML source.

Sumanth16
Kilo Patron

Hi @Pavan Dev ,

 

Create business rule with below script

Sumanth16_0-1710625052735.png

 

 

Sumanth16_1-1710625052710.png

 

 

see this

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0721958

 

If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!

 

Thanks & Regards,

Sumanth Meda