- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2024 10:13 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2024 10:48 PM
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(/</g, '<')
.replace(/>/g, '>')
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/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;
🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2024 10:26 AM
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:
var htmlText = "Test's";
var decodedText = htmlText.replace(/&#(\d+);/g, function(match, dec) {
return String.fromCharCode(dec);
}).replace(/<.*?>/g, '').trim();
gs.print(decodedText); // Output: Test's
🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2024 09:43 AM
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 !@#$%^&*()_+=-\|}]{[:;"'<,>.?/ 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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2024 10:48 PM
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(/</g, '<')
.replace(/>/g, '>')
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/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;
🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2024 10:34 AM
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