Last active
June 8, 2018 14:37
-
-
Save blubbll/b2c72fa21fe519255c0c8e963699e783 to your computer and use it in GitHub Desktop.
This file contains 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'); | |
// regular expression matching HTML entities | |
var entity = /&(?:#x[a-f0-9]+|#[0-9]+|[a-z0-9]+);?/ig; | |
return function decodeHTMLEntities(str) { | |
// find and replace all the html entities | |
str = str.replace(entity, function(m) { | |
element.innerHTML = m; | |
return element.textContent; | |
}); | |
// reset the value | |
element.textContent = ''; | |
return str; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment