Replace single quotes with an apostrophe in a string (Ruby)

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

I tried replacing them with gsub as follows:

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

None of these options work - getting an error:

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

Are they reserved by Ruby? How to replace them?

+6
source share
3 answers

If your text editor does not support UTF-8 characters, such as ' , you can avoid them as follows:

 "\u2018" 

So in your example, this would be:

 result.gsub(/\u2018/, "'") 
+6
source

Try:

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

It should work.

+1
source

In addition to what @kiplantt said, it also works (just tested):

 puts result.gsub(/(\`)/, "\\'") 
0
source

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


All Articles