Skip to content

Instantly share code, notes, and snippets.

@mark05e
Created January 12, 2022 21:32
Show Gist options
  • Save mark05e/bb479e883605567e63c05fbdaaff9a22 to your computer and use it in GitHub Desktop.
Save mark05e/bb479e883605567e63c05fbdaaff9a22 to your computer and use it in GitHub Desktop.
Decode HTML entities in a provided string.
//https://stackoverflow.com/questions/5796718/html-entity-decode
var decodeEntities = (function() {
// this prevents any overhead from creating the object each time
var element = document.createElement('div');
function decodeHTMLEntities (str) {
if(str && typeof str === 'string') {
// strip script/html tags
str = str.replace(/<script[^>]*>([\S\s]*?)<\/script>/gmi, '');
str = str.replace(/<\/?\w(?:[^"'>]|"[^"]*"|'[^']*')*>/gmi, '');
element.innerHTML = str;
str = element.textContent;
element.textContent = '';
}
return str;
}
return decodeHTMLEntities;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment