0

How can I decode a url in JavaScript or JQuery?


An example: I've got this url:

http://localhost:8080/map/file.html?var1=a%20b%20c&var2=d%20e%20f

If I use code below,

var str = "var1=a%20b%20c&var2=d%20e%20f";
document.write(str.replace("%20", " "));

I've got this:

http://localhost:8080/map/file.html?var1=a b%20c&var2=d%20e%20f

What is the fast en the best way to decode the url?

H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
  • 1
    Possible duplicate of [Replacing all occurrences of a string in JavaScript](http://stackoverflow.com/questions/1144783/replacing-all-occurrences-of-a-string-in-javascript) – JJJ Nov 24 '15 at 13:53
  • 2
    @Juhana While that does do what the OP is asking, I think Tushar's answer is probably what the OP actually needs – James Thorpe Nov 24 '15 at 13:54
  • 1
    Yeah, although the actual problem has more than its share of duplicates too... – JJJ Nov 24 '15 at 13:55
  • 1
    Tushars answer is the correct one, since it is a URL and you are trying to decode the encoded URI – EasyBB Nov 24 '15 at 14:03
  • @Juhana: I've edit my question, it was not so clear that is was an url – H. Pauwelyn Nov 24 '15 at 14:04

2 Answers2

8

Use decodeURIComponent

var str = decodeURIComponent("This%20is%20a%20%string");

As the string is URL-encoded, the last occurrence of %20 contains an extra % after it, which is probably a typo.

var str = decodeURIComponent("This%20is%20a%20string");
document.write(str);

EDIT:

var url = "http://localhost:8080/map/file.html?var1=a%20b%20c&var2=d%20e%20f";
document.write(decodeURIComponent(url));
Tushar
  • 85,780
  • 21
  • 159
  • 179
  • This will fail due to `%20%` – Alex K. Nov 24 '15 at 13:57
  • @AlexK. I guess that is copy-paste error as the string is clearly encoded string. – Tushar Nov 24 '15 at 13:58
  • Well that's not an actual URI Component then. – EasyBB Nov 24 '15 at 13:58
  • and it will replace a ton off other stuff, wich he might not know – CoderPi Nov 24 '15 at 13:59
  • Aye it probably is a typo, but worth mentioning – Alex K. Nov 24 '15 at 13:59
  • @EasyBB he didnt ask for replacing an URI Component – CoderPi Nov 24 '15 at 13:59
  • From the looks of the question, this would be the real answer. Now if the OP did not want to use `%20` as an example they should change the question. – EasyBB Nov 24 '15 at 14:00
  • 3
    @CodeiSir That's because this is probably an [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), and Tushar's answer deals with the actual underlying issue, not the immediate need. – James Thorpe Nov 24 '15 at 14:00
  • @JamesThorpe but then you still have to explain it - like this it might only confuse him. This is the kind of answers that are made AS FAST AS POSSIBLE to earn Credits, not to provide a good solution – CoderPi Nov 24 '15 at 14:01
  • @CodeiSir though I agree with you, which is why I gave you the upvote and marked this question as a duplicate. This from seeing `%20` which everyone knows is a space in the URI, so we'd automagically assume it is decoding. – EasyBB Nov 24 '15 at 14:01
  • 1
    I expected this as well, that why I gave you an upvote ;) – CoderPi Nov 24 '15 at 14:03
3

You are only replacing the first element. Use this RegEx with g to replace all:

var str = "This%20is%20a%20%string";
console.log(str.replace(/%20/g, " ");

EDIT: as after your edit it appears, you want to unescape an URI use decodeURIComponent() as first suggested by @EasyBB

CoderPi
  • 12,985
  • 4
  • 34
  • 62
  • as for this answer `g` is a global modifier of the RegExp expressions. Just like there is `m` == multiline, `i` == case insensitive... – EasyBB Nov 24 '15 at 14:05