Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 495 Vote(s) - 3.56 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I escape a single quote in Ruby?

#1
I am passing some JSON to a server via a script (not mine) that accepts the JSON as a string.

Some of the content of the JSON contains single quotes so I want to ensure that any single quotes are escaped before being passed to the script.

I have tried the following:

> irb
> 1.9.3p194 :001 > x = "that's an awesome string"
> => "that's an awesome string"
> 1.9.3p194 :002 > x.sub("'", "\'")
> => "that's an awesome string"
> 1.9.3p194 :003 > x.sub("'", "\\'")
> => "thats an awesome strings an awesome string"

but can't seem to get the syntax right.
Reply

#2
Try this way, use backslash to escape:

puts 'sean\'s'

output should be:

sean's
Reply

#3
It's likely that if you are trying to escape a single quote (apostrophe), you want to also escape double quotes. (This would apply for Javascript/JSON)

The easiest way is to use `escape_javascript`

[To see links please register here]

it "Escapes carriage returns and single and double quotes for JavaScript segments."

NOTE this works only for **Ruby on Rails**, not plain Ruby. However you can make a polyfill for plain Ruby using something like this:


JS_ESCAPE_MAP = { '\\' => '\\\\', '</' => '<\/', "\r\n" => '\n', "\n" => '\n', "\r" => '\n', '"' => '\\"', "'" => "\\'" }

def escape_javascript(javascript)
if javascript
result = javascript.gsub(/(\|<\/|\r\n|\342\200\250|\342\200\251|[\n\r"'])/u) {|match| JS_ESCAPE_MAP[match] }
else
''
end
end

Reply

#4
The reason `sub("'", "\'")` does not work is because `"\'"` is the same as `"'"`. Within double quotes, escaping of a single quote is optional.

The reason `sub("'", "\\'")` does not work is because `"\\'"` expands to a backslash followed by a single quote. Within `sub` or `gsub` argument, a backslash followed by some characters have special meaning comparable to the corresponding global variable. Particularly in this case, the global variable `$'` holds the substring after the last matching point. Your `"\\'"` within `sub` or `gsub` argument position refers to a similar thing. In order to avoid this special convention, you should put the replacement string in a block instead of an argument, and since you want to match not just one, you should use `gsub` instead of `sub`:

gsub("'"){"\\'"}
Reply

#5
Why aren't you using the JSON gem?

require 'json'
some_object = {'a string' => "this isn't escaped because JSON handles it.", 'b' => 2}

puts some_object.to_json
=> {"a string":"this isn't escaped because JSON handles it.","b":2}

And a round-trip example:

require 'pp'
pp JSON[some_object.to_json]
=> {
"a string" => "this isn't escaped because JSON handles it.",
"b" => 2
}

And an example with double-quotes:

some_object = {
'a string' => "this isn't escaped because JSON handles it.",
'another string' => 'double-quotes get "escaped"'
}
puts some_object.to_json
=> {
"a string" => "this isn't escaped because JSON handles it.",
"another string" => "double-quotes get \"escaped\""
}
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through