mbaed
mbaed

Reputation: 11

How to decode a string with unicode and utf-8 in javascript?

Ok please help - I am having trouble unescaping all the unicode/utf-8 in my javascript string. I looked at this question but I don't understand how to form a regex that covers everything I need. For example, a string I have might be: "\xe2\x80\x98my\u002c union" and I would want an output of 'my, union. I am very very confused about how to deal with this format, I tried looking at this resource but I just don't understand how to use or construct a regex. Also, if there is another much easier way to unescape these characters, ideas would be welcome, I am a total beginner at this. I did try unescape(JSON.parse(mystring)) but this did not work either. Please help!!

Upvotes: 1

Views: 434

Answers (1)

sfletche
sfletche

Reputation: 49714

You can use decodeURIComponent with escape, as detailed in this blogpost

decodeURIComponent(escape('\xe2\x80\x98my\u002c union')) // "‘my, union"

Upvotes: 2

Related Questions