Skip to content

Instantly share code, notes, and snippets.

@dtinth
Created August 14, 2011 15:01
Show Gist options
  • Save dtinth/1144953 to your computer and use it in GitHub Desktop.
Save dtinth/1144953 to your computer and use it in GitHub Desktop.
Decode UTF-8 characters in URLs. Pass the second argument as true to make it output as &#charcode; instead of the character.
function decodeUTF(x, z) {
return x.replace(/%([cd][0-9a-f]%[89ab][0-9a-f]|e[0-9a-f](?:%[89ab][0-9a-f]){2}|f[0-7](?:%[89ab][0-9a-f]){3}|f[89ab](?:%[89ab][0-9a-f]){4}|f[cd](?:%[89ab][0-9a-f]){5})/ig, function(a) {
return z ? '&#' + decodeURIComponent(a).charCodeAt(0) + ';' : decodeURIComponent(a);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment