Skip to content

Instantly share code, notes, and snippets.

Created September 15, 2010 09:49
Show Gist options
  • Save anonymous/580493 to your computer and use it in GitHub Desktop.
Save anonymous/580493 to your computer and use it in GitHub Desktop.
function mgDecodeHTMLEntities(str)
{
//substitutes html numeric entities with the corresponding unicode character in one pass
var currPos = 0;
var d = str;
var finalStr = '';
while (true)
{
var start = d.indexOf('&#', currPos);
if (start == -1)
break;
var end = d.indexOf(';', start);
if (end == -1)
break;
var expr = d.substr(start + 2, end - start - 2);
//handle hexadecimal values
if (expr.substr(0, 1) == 'x')
{
expr = parseInt(expr.substr(1), 16);
}
else
{
expr = parseInt(expr);
}
if(expr >= -32768 && expr <= 65535)
{
expr = String.fromCharCode(expr);
}
else
{
expr = '';
}
finalStr = finalStr + d.substr(currPos, start - currPos) + expr;
currPos = end + 1;
}
finalStr = finalStr + d.substr(currPos);
return finalStr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment