javascript 在 NodeJS 中 urldecode (php) 的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15854953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:20:21  来源:igfitidea点击:

Best way to urldecode (php) in NodeJS

javascriptnode.jsurldecode

提问by cyberwombat

So I am trying to decode a string that was previously urlencoded with php in Node. About a month ago I had it working with:

所以我试图解码一个之前在 Node.js 中用 php urlencoded 的字符串。大约一个月前,我与它一起工作:

querystring.unescape(str.replace(/\+/g, '%20'));

Then it just stopped working - not sure if it was some Node upgrade or what. After playing around it seems I can just use 'unescape()' but I am not sure it if it's foolproof yet.

然后它就停止工作了 - 不确定是不是 Node 升级还是什么。在玩弄之后,我似乎只能使用“unes​​cape()”,但我不确定它是否万无一失。

unescape(str.replace(/\+/g, '%20'));

My question is what is the best way and has anyone else noticed this issue. Note that the first line works with simple strings but breaks down with odd characters - so maybe some encoding issue I am not seeing.

我的问题是最好的方法是什么,有没有其他人注意到这个问题。请注意,第一行适用于简单的字符串,但会分解为奇数字符 - 所以也许我没有看到一些编码问题。

Here's a string:

这是一个字符串:

%E6.%82%CCI-T%8C%01+A

%E6.%82%CCI-T%8C%01+A

Now go to http://www.tareeinternet.com/scripts/unescape.htmland decode it. That is my original (it's an RC4 encrypted string). I want Node to return that string.

现在转到http://www.tareeinternet.com/scripts/unescape.html并对其进行解码。那是我的原件(它是一个 RC4 加密字符串)。我希望 Node 返回那个字符串。

回答by Golo Roden

If you just use the unescapefunction that's built in into Node.js, your result should be what you want.

如果您只是使用unescape内置于 Node.js 中的函数,那么您的结果应该就是您想要的。

Using Node.js 0.10.1 and running

使用 Node.js 0.10.1 并运行

unescape('%E6.%82%CCI-T%8C%01+A');

on the interactive shell, I get

在交互式外壳上,我得到

'?.ìI-T\u0001+A'

as result which looks pretty much like what you would like to get.

结果看起来很像你想要的。

Hope this helps :-)

希望这可以帮助 :-)