Skip to content

Instantly share code, notes, and snippets.

@simongong
Created May 10, 2016 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simongong/00f223537e9df5faba3f3b97ee180b7e to your computer and use it in GitHub Desktop.
Save simongong/00f223537e9df5faba3f3b97ee180b7e to your computer and use it in GitHub Desktop.
JavaScript: decode a string in browser which is html-entity-encoded
// originally from: http://stackoverflow.com/questions/5796718/html-entity-decode/27385169
function(str) {
var element = document.createElement('div');
// regular expression matching HTML entities
var entity = /&(?:#x[a-f0-9]+|#[0-9]+|[a-z0-9]+);?/ig;
function decodeHTMLEntities() {
// 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;
}
return decodeHTMLEntities(str);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment