0

According to this:

http://en.wikipedia.org/wiki/Query_string#URL_encoding

"+" is a valid URL encoding token.

If so, why can't decodeURIComponent or decodeURI decode "hello+world" to "hello world"?

If "+" is valid, surely, there has to be a built-in function in JavaScript that can convert "hello+world" to "hello world"?

corgrath
  • 11,673
  • 15
  • 68
  • 99
  • 1
    a solution and explanation has already been provided here: http://stackoverflow.com/questions/12042592/decoding-url-parameters-with-javascript – Sim1 Mar 06 '15 at 10:06

2 Answers2

2

The behavior of decideURIComponent is defined as "inverse" operation of encodeURIComponent:

The decodeURIComponent function computes a new version of a URI in which each escape sequence and UTF-8 encoding of the sort that might be introduced by the encodeURIComponent function is replaced with the character that it represents.

And encodeURIComponent does not replace spaces with +, but with %20.

(similar for decodeURI)

If "+" is valid, surely, there has to be a built-in function in JavaScript that can convert "hello+world" to "hello world"?

Of course there is:

"hello+world".replace(/\+/g, ' ');
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
0

Because encodeURIComponent would encode a space to %20 so you would get hello%20world. If you want to replace + characters I would suggest using regex

Joe Fitter
  • 1,309
  • 7
  • 11