Skip to content

Instantly share code, notes, and snippets.

@zyzsdy
Last active February 5, 2016 05:28
Show Gist options
  • Save zyzsdy/1f2e59a2a23fe1af4ee4 to your computer and use it in GitHub Desktop.
Save zyzsdy/1f2e59a2a23fe1af4ee4 to your computer and use it in GitHub Desktop.
function convert(str){
function getUTF8Length(first3){
var code = parseInt(first3.substr(1), 16);
if((code >> 7 & 1) == 0) return 1;
if((code >> 6 & 1) == 0) return 0;
if((code >> 5 & 1) == 0) return 2;
if((code >> 4 & 1) == 0) return 3;
if((code >> 3 & 1) == 0) return 4;
return 0;
}
return str.replace(/(\.[A-F0-9]{2})+/g, function($0){
var res = "";
for(var i = 0; i < $0.length;){
var thisstr = $0.substr(i, 3);
var ul = getUTF8Length(thisstr);
if(ul == 0){
res += thisstr;
i += 3;
continue;
}else{
res += decodeURIComponent($0.substr(i, 3 * ul).replace(/\./g, '%'));
i += 3 * ul;
}
}
return res;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment