测试方法:
1
|
alert( "你好" .replace(/[^\u0000-\u00FF]/g, function ($0){ return escape($0).replace(/(%u)(\w{4})/gi, "\\u$2" )}))
|
将其封装为函数:
1
2
3
4
5
|
function toUnicode(str){
return str.replace(/[^\u0000-\u00FF]/g, function ($0){
return escape($0).replace(/(%u)(\w{4})/gi, "\\u$2" );
});
}
|