Javascript 点滴记录(一)

经常会用到正则,要验证中文,就找了点关于unicode的资料

字符串转 UCS2

var unicode = '', str = '中文';
for (var i=0; i<str.length; i++) {
    unicode += '\\u'+str.charCodeAt(i).toString(16);
}
console.log(unicode); // => \u4e2d\u6587


UCS2 转字符串

var s = "\u4e2d\u6587";
s.toString(); // => 中文


另:CJK字符集从\u4e00-\u9fff,具体参见
http://unicode.org/charts/
http://www.unicode.org/charts/PDF/U4E00.pdf
http://en.wikipedia.org/wiki/Han_unification

你可能感兴趣的:(JavaScript,unicode)