JS汉字编码转换

代码
function ascii(str){
return str.replace(/[^\u0000-\u00FF]/g,function($0){return escape($0).replace(/(%u)(\w{4})/gi,"\&#x$2;")});
}

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;
}
//js对url参数中的汉字编码和解码

 

posted @ 2010-05-26 16:17  咸鱼公子  Views(3930)  Comments(0Edit  收藏  举报