#decoding a unicode string in node.js

1 messages · Page 1 of 1 (latest)

winter vapor
#

that's not what URI does

#

those are just unicode escapes

#

are you recieving the former as a string?

#

as in, the backslashes are double escaped

#

because that wouldn't work in browser either

#

then those strings are equivalent

#

can you just show your issue

#

you seem to be misunderstanding your problem

#

decodeURI is not relevant here

winter vapor
#

what's the code you're running

#
"\\"      // value \
"\\u002b" // value \u002b
"\u002b"  // value +
#

yeah that would happen in the browser as well

#

those are double escaped

winter vapor
#

so you need to turn \\uXXXX into \uXXXX
you could use regex or i guess technically eval to do that (but don't use eval)

#

give me a sec, ill check if there's a more direct way

#

that's not how strings or regex work

winter vapor
#

that's just not how any language works

#

looks like JSON.parse is a usable replacement for eval here, since it'll process the escapes but safely

#

console.log(JSON.parse(example))

#

a regex solution would be example.replace(/\\u(?:\d{4}|\{\d+\})/g, (match) => String.fromCodePoint(Number(match.match(/\d+/)[0])))

#

that doesn't help me help you

#

how doesn't it work

#

ah yeah you need to enclose it with "

#

forgot to tell you

#

with that it works just fine