Skip to content

Instantly share code, notes, and snippets.

@blubbll
Last active June 8, 2018 14:37
Show Gist options
  • Save blubbll/b2c72fa21fe519255c0c8e963699e783 to your computer and use it in GitHub Desktop.
Save blubbll/b2c72fa21fe519255c0c8e963699e783 to your computer and use it in GitHub Desktop.
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