关于url:为什么” decodeURIComponent”或” decodeURI”无法在JavaScript中将” hello world”解码为” hello world”?

Why can't “decodeURIComponent” or “decodeURI” decode “hello+world” to “hello world” in JavaScript?

本问题已经有最佳答案,请猛点这里访问。

据此:

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

""是有效的URL编码令牌。

如果是,为什么decodeURIComponentdecodeURI不能将" hello world"解码为" hello world"?

如果""有效,那么肯定有JavaScript中的内置函数可以将" hello world"转换为" hello world"吗?


decideURIComponent的行为定义为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.

并且encodeURIComponent不会用+代替空格,而是%20

(与decodeURI类似)

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

当然有:

1
"hello+world".replace(/\\+/g, ' ');

因为encodeURIComponent会将一个空格编码为%20,所以您将得到hello%20world。如果要替换+字符,我建议使用regex