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

I am writing a String class extension method to clear non-ASCII characters. The strings I clean are UTF-8.

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

How to avoid curly quote in gsub?

How to write gsub that uses unicode for curly quotes (e.g. U + 201C).

Work in Rails 3.07 and Ruby 1.9.2.

+6
source share
1 answer

You can use the same \u escapes in regular expressions that you would use in double quotes:

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

For instance:

 >> s = "\u201Cpancakes\u201d" => ""pancakes"" >> puts s.gsub(/[\u201c\u201d]/, '"') "pancakes" 
+14
source

Source: https://habr.com/ru/post/908570/


All Articles