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(/(\|<\/|\r\n|\342\200\250|\342\200\251|[\n\r"'])/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