EXACT Ruby equivalent to Javascript escape () function

Consider the line: ` ( ?

Javascript escape() encodes it like this:

 escape("` ( ?") "%60%20%28%20%3F" 

How can I achieve the same effect in Ruby? Nothing trying to work:

 [Dev]> CGI.escape("` ( ?") => "%60+%28+%3F" [Dev]> URI.encode("` ( ?") => "%60%20(%20?" [Dev]> Addressable::URI.encode("` ( ?") => "%60%20(%20?" 
+4
source share
2 answers

ERB::Util.url_encode will do this:

 >> require 'erb' => true >> ERB::Util.url_encode("` ( ?") => "%60%20%28%20%3F" 
+5
source

URI::encode also accepts a regular expression to match unsafe characters that must be escaped; you can simply pass in a regular expression matching any character:

 URI.encode("` ( ?", /./) # => "%60%20%28%20%3F" 

By the way, from the Mozilla Developer Network :

The escape and unescape functions do not work properly for non-ASCII characters and are deprecated. In JavaScript 1.5 and later, use encodeURI, decodeURI, encodeURIComponent, and decodeURIComponent.

+4
source

All Articles