Created
January 31, 2022 16:05
-
-
Save mark05e/f73d7ae6836609398ae7f03900b6e393 to your computer and use it in GitHub Desktop.
Decode HTML Entities
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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