Javascript unicode;和\uxxxx编码的转换
2009-06-30 05:09:00 来源:WEB开发网JS-unicode;编码转换
程序代码
<script type="text/javascript">
var toHTML = {
on: function(str) {
var a = [],
i = 0;
for (; i < str.length;) a[i] = str.charCodeAt(i++);
return "" + a.join(";") + ";"
},
un: function(str) {
return str.replace(/(x)?([^&]{1,5});?/g,
function(a, b, c) {
return String.fromCharCode(parseInt(c, b ? 16 : 10))
})
}
};
alert(toHTML.on("\"请输\""));
alert(toHTML.un("大幅"));
</script>
JS-unicode编码转换
程序代码
<script type="text/Javascript">
var toUN = {
on: function(str) {
var a = [],
i = 0;
for (; i < str.length;) a[i] = ("00" + str.charCodeAt(i++).toString(16)).slice( - 4);
return "\\u" + a.join("\\u")
},
un: function(str) {
return unescape(str.replace(/\\/g, "%"))
}
};
alert(toUN.on("\"请输\""));
alert(toUN.un("\\u0022\\u8BF7\\u8F93\\u0022"));
</script>
Tags:Javascript unicode uxxxx
编辑录入:爽爽 [复制链接] [打 印]赞助商链接