Cristi
Cristi

Reputation: 1328

Remove escaping for Unicode codes

I have a string with at least one occurrence of Unicode codes. The string looks something like this var unicodeIsMean = "\\u0026";.

Which is the easiest way of converting this into \u0026 (which I can then turn into &). I tried replacing \\u with \u, but it doesn't work. The simplest way so far is to straight up do string.replace("\\u0026", "&"), but I can't do this for all codes, since I'm not sure how many characters will be escaped this way.

Upvotes: 0

Views: 368

Answers (1)

Ivan Velichko
Ivan Velichko

Reputation: 6709

JSON.parse might help:

var unicodeIsMean = "\\u0026";
var decoded = JSON.parse(`{"str": "${unicodeIsMean}"}`).str;
console.log(decoded);

Upvotes: 1

Related Questions