Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从decodeURI(encodeURIComponent(originalString))恢复? 我有一些遗留代码(pre-React),在调用模块上的history.push()之前,使用encodeURIComponent对URL的一部分进行编码,以便导航到该URL history.push() 此部分解码的location.pathname在ReactRouter中结束,其中useParams()再次返回部分解码的URL组件_Javascript_Encodeuricomponent_Decodeuricomponent - Fatal编程技术网

Javascript 如何从decodeURI(encodeURIComponent(originalString))恢复? 我有一些遗留代码(pre-React),在调用模块上的history.push()之前,使用encodeURIComponent对URL的一部分进行编码,以便导航到该URL history.push() 此部分解码的location.pathname在ReactRouter中结束,其中useParams()再次返回部分解码的URL组件

Javascript 如何从decodeURI(encodeURIComponent(originalString))恢复? 我有一些遗留代码(pre-React),在调用模块上的history.push()之前,使用encodeURIComponent对URL的一部分进行编码,以便导航到该URL history.push() 此部分解码的location.pathname在ReactRouter中结束,其中useParams()再次返回部分解码的URL组件,javascript,encodeuricomponent,decodeuricomponent,Javascript,Encodeuricomponent,Decodeuricomponent,现在我被一个无法使用的部分解码的URL组件卡住了。我需要完全解码 我不能在部分解码的字符串上使用decodeURIComponent,因为原始字符串可能包含%,在这种情况下,此%将在部分解码的字符串中解码,这将导致decodeURIComponent崩溃,并出现未捕获的URIError:URI格式错误 我的选择似乎是: 使用unescape完全解码部分解码的字符串(它不会抱怨单个%),即使不鼓励使用它(为什么?) 手动将任何%(后面没有数字和后续十六进制字符)重新编码回%25,然后通过解码组件

现在我被一个无法使用的部分解码的URL组件卡住了。我需要完全解码

我不能在部分解码的字符串上使用
decodeURIComponent
,因为原始字符串可能包含
%
,在这种情况下,此
%
将在部分解码的字符串中解码,这将导致
decodeURIComponent
崩溃,并出现
未捕获的URIError:URI格式错误

我的选择似乎是:

  • 使用
    unescape
    完全解码部分解码的字符串(它不会抱怨单个
    %
    ),即使不鼓励使用它(为什么?)
  • 手动将任何
    %
    (后面没有数字和后续十六进制字符)重新编码回
    %25
    ,然后通过
    解码组件运行结果
有没有我还没有想到的不那么丑陋的解决方案

编辑:有人问我部分决定的字符串是什么意思

const original = 'A-Za-z0-9;,/?:@&=+$-_.!~*()#%';
const encodedURIComponent = encodeURIComponent(original); // "A-Za-z0-9%3B%2C%2F%3F%3A%40%26%3D%2B%24-_.!~*()%23%25"
console.log(decodeURIComponent(encodedURIComponent)); //  "A-Za-z0-9;,/?:@&=+$-_.!~*()#%"
const partiallyUnescaped = decodeURI(encodedURIComponent); // "A-Za-z0-9%3B%2C%2F%3F%3A%40%26%3D%2B%24-_.!~*()%23%" - notice the '%25' at the end was decoded back to '%'
console.log(unescape(partiallyUnescaped)); // "A-Za-z0-9;,/?:@&=+$-_.!~*()#%"
//console.log(decodeURIComponent(partiallyUnescaped)); // error
编辑2:如果有任何帮助,这里有一个更现实的例子,说明我们的URL可能包含的一些字符,但因为它是用户生成的,所以它可能是任何真正的字符:

  console.log(                             encodeURIComponent('abcd+%;- efgh'))  ; // "abcd%2B%25%3B-%20efgh"
  console.log(                   decodeURI(encodeURIComponent('abcd+%; -efgh'))) ; // "abcd%2B%%3B- efgh"
//console.log(decodeURIComponent(decodeURI(encodeURIComponent('abcd+%; -efgh')))); // Error: URI malformed

不知道你所说的“部分”是什么意思。你能举个例子吗?不幸的是,我认为这是不可能的。我认为字符串变得模棱两可了,不是吗?@apena我添加了一个我手头的示例。。。也许不是最好的,但我希望它显示了解码“%”符号的主要问题。
unescape
或手动解码器也不会有帮助,想象一下您的输入包含字面上的
%25
,encodedURIComponent将使其成为
%2525
,然后
decodeURI
将使其成为
%25
,最后
unescape
将其转换为
%
,从而丢失原始的
%25
。现在,请您澄清您的“遗留代码”编码的URL的哪些部分?这就是您需要解决的问题,因此它只正确编码所需的部分。(注意:查看lib的repo,不确定“部分”是什么意思。你能举个例子吗?不幸的是,我认为这是不可能的。我认为字符串变得模棱两可,不是吗?@apena我添加了一个我手头的例子…可能不是最好的,但我希望它显示了解码“%”符号的主要问题。
unescape
或mAnualDecoder也不会有帮助,想象一下你的输入包含字面上的
%25
,EncodeDuricComponent会使它成为
%2525
,然后
decodeURI
会使它成为
%25
,最后
unescape
会将它转换成
%
,丢失原来的
%25
。现在,你能澄清一下是什么吗您的“遗留代码”正在编码的URL的rts?这就是您需要修复的,以便它只正确编码所需的部分。(注意:查看库的repo,