上述方法解码数字会出现错误
\u9519\u8bef\u8d85\u8fc7\u35\u6b21\uff0c\u5173\u95ed\u6d4f\u89c8\u5668\u91cd\u65b0\u767b\u5f55\uff01 用以下的方法,不会出现此类问题:
- function reconvert(str){
- str = str.replace(/(\\u)(\w{1,4})/gi,function($0){
- return (String.fromCharCode(parseInt((escape($0).replace(/(%5Cu)(\w{1,4})/g,"$2")),16)));
- });
- str = str.replace(/(&#x)(\w{1,4});/gi,function($0){
- return String.fromCharCode(parseInt(escape($0).replace(/(%26%23x)(\w{1,4})(%3B)/g,"$2"),16));
- });
- str = str.replace(/(&#)(\d{1,6});/gi,function($0){
- return String.fromCharCode(parseInt(escape($0).replace(/(%26%23)(\d{1,6})(%3B)/g,"$2")));
- });
- return str;
- }
复制代码
|