Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.33 KB | None | 0 0
  1. ESCAPE_MAP   =   { '"' => '\\"', "'" => "\\'" }
  2.  
  3. def escape_quotes(str)
  4.   if str
  5.     result = str.gsub(/(\|<\/|\r\n|\342\200\250|\342\200\251|[\n\r"'])/u) {|match| ESCAPE_MAP[match] }
  6.  else
  7.    ''
  8.  end
  9. end
  10.  
  11.  
  12. string = 'double-quotes "escaped"'
  13. escapedStr = escape_quotes(string)
  14.  
  15. puts escapedStr
  16. => double-quotes \"escaped\"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement