中文-Ascii-Unicode的转换
中文、Ascii和Unicode之间的相互转换:
// Ascii编码
function ascii(str){
return str.replace(/[^\u0000-\u00FF]/g,function($0){return escape($0).replace(/(%u)(\w{4})/gi,"\&#x$2;")});
}
// Unicode编码
function unicode(str){
return str.replace(/[^\u0000-\u00FF]/g,function($0){return escape($0).replace(/(%u)(\w{4})/gi,"\\u$2")});
}
// 中文
function reconvert(str){
str = str.replace(/(\\u)(\w{4})/gi,
function($0){
return (String.fromCharCode(parseInt((escape($0).replace(/(%5Cu)(\w{4})/g,"$2")),16)));
});
str = str.replace(/(&#x)(\w{4});/gi,
function($0){
return String.fromCharCode(parseInt(escape($0).replace(/(%26%23x)(\w{4})(%3B)/g,"$2"),16));
});
return str;
}