为什么 URI.escape 不转义单引号?
为什么 URI.escape
不转义单引号?
URI.escape("foo'bar\" baz")
=> "foo'bar%22%20baz"
Why doesn't URI.escape
escape single quotes?
URI.escape("foo'bar\" baz")
=> "foo'bar%22%20baz"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
出于同样的原因,它不会转义
?
或/
或:
等等。URI.escape()
仅转义根本不能在 URL 中使用的字符,而不是具有特殊含义的字符。您要查找的是
CGI.escape()
:For the same reason it doesn't escape
?
or/
or:
, and so forth.URI.escape()
only escapes characters that cannot be used in URLs at all, not characters that have a special meaning.What you're looking for is
CGI.escape()
:这是一个老问题了,但答案已经很长时间没有更新了。我想我会为其他遇到同样问题的人更新此内容。我找到的解决方案是发布在此处:如果您有的话,请使用
ERB::Util.url_encode
erb 模块可用。这解决了单引号和单引号的问题。*
对我来说也是如此。与加号相比,
CGI::escape
无法正确转义空格 (%20
)。This is an old question, but the answer hasn't been updated in a long time. I thought I'd update this for others who are having the same problem. The solution I found was posted here: use
ERB::Util.url_encode
if you have theerb
module available. This took care of single quotes &*
for me as well.CGI::escape
doesn't escape spaces correctly (%20
) versus plus signs.根据文档,
URI.escape(str [, unsafe])
使用正则表达式来匹配必须用代码替换的所有符号。默认情况下,该方法使用 REGEXP::UNSAFE。当这个参数是一个字符串时,它代表一个字符集。在您的情况下,要修改
URI.escape
来转义单引号,您可以执行以下操作...说明:有关规范的一些信息...
According to the docs,
URI.escape(str [, unsafe])
uses a regexp that matches all symbols that must be replaced with codes. By default the method uses REGEXP::UNSAFE. When this argument is a String, it represents a character set.In your case, to modify
URI.escape
to escape even the single quotes you can do something like this ...Explanation: Some info on the spec ...
我知道这个问题已经得到解答,但我想要的东西略有不同,我想我不妨将其发布:我想保留网址中的“/”,但转义所有其他非标准字符。我是这样做的:
当我为 AWS Cloudfront 编写缓存失效时,我需要转义单引号,AWS Cloudfront 不喜欢单引号并希望它们被转义。上面应该创建一个比标准 URI.escape 更安全的 uri,但它看起来仍然像一个 URI(CGI Escape 通过转义“/”来破坏 uri 格式)。
I know this has been answered, but what I wanted was something slightly different, and I thought I might as well post it up: I wanted to keep the "/" in the url, but escape all the other non-standard characters. I did it thus:
I needed to escape the single quote as I was writing a cache invalidation for AWS Cloudfront, which didn't like the single quotes and expected them to be escaped. The above should make a uri which is more safe than the standard URI.escape but which still looks like a URI (CGI Escape breaks the uri format by escaping "/").