Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace Single Quotes with Apostrophe in String (Ruby)

Tags:

ruby

My users sometimes enter, instead of apostrophe ('), symbols that look like apostrophe (), which causes some problems with database.

I tried to replace them with gsub like so:

result.gsub(/\‘/, "'")
result.gsub(/‘/, "'")

Neither of these options work - getting the error:

syntax error, unexpected $end, expecting ')'
  return result.gsub(/\‘/, "'").gsub("’", "'")
                          ^

Are they reserved by Ruby? How do I replace them?

like image 301
Serge Vinogradoff Avatar asked May 07 '13 13:05

Serge Vinogradoff


2 Answers

If your text editor doesn't support UTF-8 characters like directly, you can escape them this way:

"\u2018"

So in your example, it would be:

result.gsub(/\u2018/, "'")
like image 105
kiplantt Avatar answered Nov 15 '22 04:11

kiplantt


Try:

result.gsub("‘", "'")

It should work.

like image 23
Deepika Avatar answered Nov 15 '22 05:11

Deepika