为什么 "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"?

The behavior of decideURIComponent is defined as "inverse" 运算 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"?

当然有:

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

因为 encodeURIComponent 会将 space 编码为 %20,所以你会得到 hello%20world。如果你想替换 + 个字符,我建议使用正则表达式