Join us Dec. 16 for The Community Hour, our first global virtual User Group event!

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;

 

🙂

View solution in original post

6 REPLIES 6

Sohithanjan G
Kilo Sage

Hi @Pavan Dev 

It seems like you're trying to replace HTML entities with their corresponding characters in a string. However, your current code only removes HTML tags, but it doesn't handle HTML entities conversion. You can use a function to decode HTML entities. Here's how you can do it in Javascript&colon;

var htmlText = "Test&#39;s";
var decodedText = htmlText.replace(/&#(\d+);/g, function(match, dec) {
    return String.fromCharCode(dec);
}).replace(/<.*?>/g, '').trim();

gs.print(decodedText); // Output: Test's

 

 🙂

Hi @Sohithanjan G 

Thanks for the response.

 

The code is working fine except for certain special characters. Below is the code

Originial HTML = 'Test !@#$%^&*()_+=-\|}]{[:;"'<,>.?/ Test1';

Converted String = Test !@#$%^&amp;*()_+=-\|}]{[:;"'&lt;,&gt;.?/ Test1;

var htmlText = current.u_html_field;
var decodedText = htmlText.replace(/&#(\d+);/g, function(match, dec) {
return String.fromCharCode(dec);
}).replace(/<.*?>/g, '').trim();

return decodedText;

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;

 

🙂

Not applicable

Hi @Pavan Dev 

 

Please visit the following community question-

https://www.servicenow.com/community/developer-forum/remove-html-tags/td-p/1446179

 

Hope this helps.

 

Regards,

Amit