- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-02-2023 06:42 AM
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2023 01:30 AM
Hi @divyamodi
To convert only Unicode HTML characters you can use the following script.
var html = "<p>Anvesh ✘</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 ✘</p>"
var text = html.replace(/<\/?p>/gi, "\n")
.replace(/<\/?br\s*\/?>/gi, "\n")
.replace(/<li>/gi, " * ")
.replace(/(<(?:.|\n)*?>)/gm, "")
.replace(/ /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 👍✔️
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-02-2023 10:10 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2023 07:57 AM
Thanks Abbas for the quick help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2023 01:30 AM
Hi @divyamodi
To convert only Unicode HTML characters you can use the following script.
var html = "<p>Anvesh ✘</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 ✘</p>"
var text = html.replace(/<\/?p>/gi, "\n")
.replace(/<\/?br\s*\/?>/gi, "\n")
.replace(/<li>/gi, " * ")
.replace(/(<(?:.|\n)*?>)/gm, "")
.replace(/ /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 👍✔️
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2023 07:55 AM
Thanks for providing the script. It's really helpful.