7

So I am trying to decode a string that was previously urlencoded with php in Node. About a month ago I had it working with:

querystring.unescape(str.replace(/\+/g, '%20'));

Then it just stopped working - not sure if it was some Node upgrade or what. After playing around it seems I can just use 'unescape()' but I am not sure it if it's foolproof yet.

unescape(str.replace(/\+/g, '%20'));

My question is what is the best way and has anyone else noticed this issue. Note that the first line works with simple strings but breaks down with odd characters - so maybe some encoding issue I am not seeing.

Here's a string:

%E6.%82%CCI-T%8C%01+A

Now go to http://www.tareeinternet.com/scripts/unescape.html and decode it. That is my original (it's an RC4 encrypted string). I want Node to return that string.

cyberwombat
  • 38,105
  • 35
  • 175
  • 251
  • It seems `unescape(str)` decodes it just like the page you mention; it looks like `querystring.unescape()` only deals with encoded UTF-8 strings, not raw byte strings. – robertklep Apr 06 '13 at 19:49

1 Answers1

29

If you just use the unescape function that's built in into Node.js, your result should be what you want.

Using Node.js 0.10.1 and running

unescape('%E6.%82%CCI-T%8C%01+A');

on the interactive shell, I get

'æ.ÌI-T\u0001+A'

as result which looks pretty much like what you would like to get.

Hope this helps :-)

Golo Roden
  • 140,679
  • 96
  • 298
  • 425
  • Well yes that is what I have above - you do need to add the replace bit as unescape by itself will eventually break on long strings - I tried. I'm just curious as to why querstring.unescape suddenly stopped working for me – cyberwombat Apr 06 '13 at 20:44