JS将unicode码转中文方法

 

 

原理,将unicode的 \u 先转为 %u,然后使用unescape方法转换为中文。

1
2
3
4
<script type="text/javascript">
 var str = "\u7434\u5fc3\u5251\u9b44\u4eca\u4f55\u5728\uff0c\u6c38\u591c\u521d\u6657\u51dd\u78a7\u5929\u3002";
 document.write(unescape(str.replace(/\\u/g, '%u'))); //琴心剑魄今何在,永夜初晗凝碧天。
</script>

记得head标记中加入

<meta http-equiv="content-type" content="text/html; charset=utf-8"> 

posted @ 2017-06-05 00:22  牧之丨  阅读(1547)  评论(1编辑  收藏  举报