使用 decodeURIComponent 解码字符串 编码方法是否有问题

Is there a problem to use decodeURIComponent to decoding the string witch coding with encode method

如题,我尝试用encodeURI方法编码一个字符串,用decodeURIComponent解码结果method.and然后我发现解码字符串和原来的一样string.So我想知道是否所有使用 encodeURI 编码的字符串都可以使用 decodeURIComponent 解码。

encodeURI("http://www.example.com?type=qqq&string=qqq+<>`www");
//"http://www.example.com?type=qqq&string=qqq+%3C%3E%60www"

decodeURIComponent("http://www.example.com?type=qqq&string=qqq+%3C%3E%60www");
//"http://www.example.com?type=qqq&string=qqq+<>`www"

这里是所有函数的输出:

输入字符串:https://www.google.co.in/

来自 encodeURI 的输出字符串:https://www.google.co.in/

来自 encodeURIComponent 的输出字符串:https%3A%2F%2Fwww.google.co.in%2F

现在你用 decodeURIdecodeURIComponent 解码 encodeURI 的结果,它会得到相同的结果。但是如果你解码 encodeURIComponent 的结果,它会给你以下结果。

输入字符串:https%3A%2F%2Fwww.google.co.in%2F

来自 decodeURI 的输出字符串:https%3A%2F%2Fwww.google.co.in%2F

来自 decodeURIComponent 的输出字符串:https://www.google.co.in/

所以结论是 decodeURIComponent 旨在解码所有内容 因此按照其规范使用它是安全的意味着 decodeURIencodeURIdecodeURIComponentencodeURIComponent.