开发者

Ruby equivalent to JavaScript’s encodeURIComponent that produces identical output? [duplicate]

This question already has answers here: How do I raw URL encode/decode in JavaScript and Ruby to get the same values in both? (3 answers) Closed 7 years ago.

Hi is there an equivalent ruby method to JavaScript encodeURIComponent method? i am using the URI.unescape(str) but it recognizes the "£" (after encodeURIComponent it bec开发者_StackOverflow社区omes "%C2%A3") as a "?" sign. any solution's? thanks


URI.escape(foo, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))

found here: How do I raw URL encode/decode in JavaScript and Ruby to get the same values in both?

URI.escape is obslete since last I posted. As per suggested comment, now use: ERB::Util.url_encode or CGI.escape


CGI.escape should escape correctly, except that spaces are escaped as +.

Note that URI.escape has been deprecated in Ruby 1.9.2...

There is a long discussion on ruby-core for those interested.


I think the simplest way is URI.encode_www_form_component.


URI.escape

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜