-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTMLUtils.js
23 lines (20 loc) · 892 Bytes
/
HTMLUtils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
ServiceNow HTML utility functions
Author: Nick Voss
Date: July 5, 2023
Description: Demonstrates some options for parsing HTML. Note that arsing HTML via regex is not a best practice,
however these functions work in many use cases. Another option is GlideSPScriptable().stripHTML()
*/
function decodeHTML(str) {
var a = str.replace(/<\/?[^>]+(>|$)/g, "");
var b = a.replace(/&/g, '&'); // Retain any ampersands that are just ampersands
return b.replace(/&#(\d+);/g, function(match, dec) {
return String.fromCharCode(dec); // Return the special character from the decimal code representation and return the entire decoded string.
});
}
function removeHTML(text) {
var lineBreakRegex = /<br\s\/>/ig;
var tagRegex = /<\/?[a-z][\s\S]*?(?:\/>|>)/ig;
var temp = text.replace(lineBreakRegex, '\r\n');
return temp.replace(tagRegex, "");
}