# Ruby GSUB question

**URL:** <https://www.ruby-forum.com/t/ruby-gsub-question/149170>\
**Category:** Ruby\
**Created:** [September 22, 2008, 8:32am UTC](https://www.ruby-forum.com/t/ruby-gsub-question/149170 "2008-09-22T08:32:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![binkly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@binkly](https://www.ruby-forum.com/u/binkly)\
**Post date:** [September 22, 2008, 8:32am UTC](https://www.ruby-forum.com/t/ruby-gsub-question/149170/1 "2008-09-22T08:32:15Z")

</div>

Hello All,

In Ruby if I have the string:

Remove

How would I go about replacing the single quotes ', with a backslash and  
single quote? ’

Ultimately I’d like to arrive at the string below:

\<a href="#" onclick="new  
Ajax.Request(’/accounts/todo\_lists/1/todo\_items/2’,  
{asynchronous:true, evalScripts:true, parameters:‘authenticity\_token=’

- encodeURIComponent(‘0523c2646f11e096ef9ecd2f6e5b41690f99fc39’)});  
return false;"\>Remove

Every variation of GSUB that I’ve tried has yet to yield that second  
string. Any assistance would be greatly appreciated.

Thank you,

Binh

---

<div class="post-metadata">

**Author:** ![binkly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@binkly](https://www.ruby-forum.com/u/binkly)\
**Post date:** [September 22, 2008, 9:17am UTC](https://www.ruby-forum.com/t/ruby-gsub-question/149170/2 "2008-09-22T09:17:13Z")

</div>

I understand the first \ , as it will produce \ when it’s displayed ,  
but why are there still 2 more \ ? As I see it , \\’ would produce  
\’ , and only when it will be printed will we see ’ . Could someone  
please explain this ?

---

<div class="post-metadata">

**Author:** ![binkly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@binkly](https://www.ruby-forum.com/u/binkly)\
**Post date:** [September 22, 2008, 9:13am UTC](https://www.ruby-forum.com/t/ruby-gsub-question/149170/3 "2008-09-22T09:13:09Z")

</div>

From: Binh Ly [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# \<a href=“#” onclick="new

# Ajax.Request(‘/accounts/todo\_lists/1/todo\_items/2’,

# {asynchronous:true,

# evalScripts:true, parameters:‘authenticity\_token=’ +

# encodeURIComponent(‘0523c2646f11e096ef9ecd2f6e5b41690f99fc39’)

# }); return

# false;"\>Remove

# How would I go about replacing the single quotes ', with a

# backslash and

# single quote? '

you can try harder 😉

puts “c’mon, it’s cool!”.gsub(“'”,“\\'”)  
c'mon, it's cool!  
#=\> nil

or simply just,

puts “c’mon, it’s cool!”.gsub(“'”){“\'”}  
c'mon, it's cool!

---

<div class="post-metadata">

**Author:** ![binkly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@binkly](https://www.ruby-forum.com/u/binkly)\
**Post date:** [September 22, 2008, 9:45am UTC](https://www.ruby-forum.com/t/ruby-gsub-question/149170/4 "2008-09-22T09:45:53Z")

</div>

Lex W. wrote:

> I understand the first \ , as it will produce \ when it’s displayed ,  
> but why are there still 2 more \ ? As I see it , \\’ would produce  
> \’ , and only when it will be printed will we see ’ . Could someone  
> please explain this ?

Inside a single- or double-quoted string, \ is an escape character, and  
to get a literal backslash you have to escape it with another backslash.  
So “\” is a single backslash; “\\” is two backslashes.

irb(main):002:0\> “\”.size  
=\> 1

Inside a regexp replacement string, backslash is also an escape  
character, because (for example) \1 means “substitute the first capture  
group”, i.e. the string inside the first set of parentheses.

irb(main):006:0\> “abc123def”.gsub(/(\d+)/, ‘_\1_’)  
=\> “abc_123_def”

So to actually substitute a backslash, the replacement string has to  
contain backslash backslash, and you need to write that as “\\” or  
‘\\’

irb(main):007:0\> “abc123def”.gsub(/\d+/, ‘\\’)  
=\> “abc\def”  
^  
this is a single backslash in the replacement

If using the block form of gsub then there is no need for the \1 syntax,  
since there are other ways of achieving the same thing, and so the  
second level of backslash escaping is disabled.

irb(main):009:0\> “abc123def”.gsub(/(\d+)/) { “_#{$1}_” }  
=\> “abc_123_def”  
irb(main):010:0\> “abc123def”.gsub(/\d+/) { ‘\’ }  
=\> “abc\def”

Of course, if all this is for escaping SQL, whatever DB access library  
you use likely has existing methods to do this for you. e.g. for  
ActiveRecord:

n = “o’reilly”  
Person.find(:all, :conditions =\> [“name=?”, n])

HTH,

Brian.

---

<div class="post-metadata">

**Author:** ![binkly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@binkly](https://www.ruby-forum.com/u/binkly)\
**Post date:** [September 22, 2008, 10:21am UTC](https://www.ruby-forum.com/t/ruby-gsub-question/149170/5 "2008-09-22T10:21:54Z")

</div>

From: Brian C. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]  
#…

# Inside a single- or double-quoted string, \ is an escape

# character, and

# to get a literal backslash you have to escape it with another

# backslash.

# So “\” is a single backslash; “\\” is two backslashes.

#…

# 

otoh, perl also has regex power tools like \Q…\E eg  
(escaping without using the backslash ie)

maybe we could have something similar?

=)

kind regards -botp

---

<div class="post-metadata">

**Author:** ![binkly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@binkly](https://www.ruby-forum.com/u/binkly)\
**Post date:** [September 22, 2008, 11:13am UTC](https://www.ruby-forum.com/t/ruby-gsub-question/149170/6 "2008-09-22T11:13:45Z")

</div>

PeÃ±a, Botp wrote:

> otoh, perl also has regex power tools like \Q…\E eg  
> (escaping without using the backslash ie)
> 
> maybe we could have something similar?
> 
> =)

Interesting; I wasn’t aware those worked inside the replacement string,  
as well as the regexp.

However, I’m not sure I’d want to copy Perl’s ugliness here. From ‘man  
perlre’:

```
   You cannot include a literal "$" or "@" within a "\Q" sequence. 

```

An  
unescaped “$” or “@” interpolates the corresponding variable,  
while  
escaping will cause the literal string “$” to be matched.  
Youâ€™ll need  
to write something like “m/\Quser\E@\Qhost/”.  
…  
Beware that if you put literal backslashes (those not inside  
interpoâ€  
lated variables) between “\Q” and “\E”, double-quotish backslash  
interâ€  
polation may lead to confusing results. If you need to use  
literal  
backslashes within “\Q…\E”, consult “Gory details of parsing  
quoted  
constructs” in perlop.

That is, if you write s/’/\Q’\E/ you’d better know what you’re doing.

I think I’ll live with /#{Regexp.escape("…")}/ instead 🙂

B.

---

<div class="post-metadata">

**Author:** ![binkly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@binkly](https://www.ruby-forum.com/u/binkly)\
**Post date:** [September 22, 2008, 12:06pm UTC](https://www.ruby-forum.com/t/ruby-gsub-question/149170/7 "2008-09-22T12:06:05Z")

</div>

From: Brian C. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# …enlightening talk…

# I think I’ll live with /#{Regexp.escape(“…”)}/ instead 🙂

you’re right. ’ totally forgot about regexp#escape as another solution  
to the op. it is much better.

kind regards -botp
