Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write gsub with curly quotes for UTF-8 strings?

I am writing an extension method for the String class to clean up non-ASCII characters. The strings that I am cleaning are UTF-8.

When using non-ASCII characters in a file, the console will not start because it is interpreting the curly quotes as regular quotes.

How to escape the curly quote in gsub?

How to write a gsub that uses the unicode for curly quotes (U+201C, for example).

Working in Rails 3.07 and Ruby 1.9.2.

like image 403
B Seven Avatar asked Feb 15 '12 23:02

B Seven


1 Answers

You can use the same \u escapes in regexes that you'd use in double quoted strings:

s.gsub(/[\u201c\u201d]/, '"')

For example:

>> s = "\u201Cpancakes\u201d"
=> "“pancakes”"
>> puts s.gsub(/[\u201c\u201d]/, '"')
"pancakes"
like image 129
mu is too short Avatar answered Sep 22 '22 08:09

mu is too short