encode - Node.js convert Chinese character to \uXXXX -
i want convert string '字符串' '\u5b57\u7b26\u4e32', how implement in node.js?
function convert(str) { //... } var s = '字符串' convert(s); // => \u5b57\u7b26\u4e32
this can done in better way (and mentioned jsesc
safer choice):
function convert(s) { return s.split('').map(function(c) { return '\\u' + ('0000' + c.charcodeat(0).tostring(16).touppercase()).slice(-4); }).join(''); } var s = '字符串'; convert(s)
Comments
Post a Comment