When I tryed to decode the string below in nodeJS using decodeURLCompnent:
var decoded = decodeURI('Ulysses%20Guimar%C3%A3es%20-%20lado%20par');
console.log(decoded);
I got
Ulysses Guimarães - lado par
Instead of
Avenida Ulysses Guimarães - lado par
But when I use the same code on the client side (browser) I can get the right char 'ã'.
Is there a way to convert from ã to ã in a Node script?
decodeURI(): It takes encodeURI(url) string as parameter and returns the decoded string. decodeURIComponent(): It takes encodeURIComponent(url) string as parameter and returns the decoded string.
The decodeURIComponent() function decodes a Uniform Resource Identifier (URI) component previously created by encodeURIComponent or by a similar routine.
The encodeURIComponent() function encodes a URI by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two "surrogate" characters).
I cannot reproduce it in 0.10 or 0.11 versions of node.
You can convert first to second using new Buffer('Ulysses Guimarães - lado par', 'binary').toString('utf8')
, but it's a workaround, not a solution.
Are you sure you're calling decodeURI, not unescape?
Use var querystring = require("querystring");
The querystring.unescape() method performs decoding of URL percent-encoded characters on the given str.
and then querystring.unescape(str)
as per docs:
https://nodejs.org/api/querystring.html#querystring_querystring_unescape_str
I'm just leaving this here, because I had the same problem. I was using the encodeURIcomponent(str)
function in the client and in Nodejs when I did decodeURI(str)
had the same problem. I solved it by using encodeURI(str)
at the client.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With