Created
May 10, 2016 06:36
-
-
Save simongong/00f223537e9df5faba3f3b97ee180b7e to your computer and use it in GitHub Desktop.
JavaScript: decode a string in browser which is html-entity-encoded
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
// 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