Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ESCAPE_MAP = { '"' => '\\"', "'" => "\\'" }
- def escape_quotes(str)
- if str
- result = str.gsub(/(["'])/u) {|match| ESCAPE_MAP[match] }
- else
- ''
- end
- end
- string = 'double-quotes "escaped"'
- escapedStr = escape_quotes(string)
- puts escapedStr
- => double-quotes \'escaped\'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement