# Problem with String.gsub and \\' as a replacement string

**URL:** <https://www.ruby-forum.com/t/problem-with-string-gsub-and-as-a-replacement-string/120482>\
**Category:** Ruby\
**Created:** [November 17, 2007, 12:20pm UTC](https://www.ruby-forum.com/t/problem-with-string-gsub-and-as-a-replacement-string/120482 "2007-11-17T12:20:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ridcully](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5f9b8f/32.png) [@ridcully](https://www.ruby-forum.com/u/ridcully)\
**Post date:** [November 17, 2007, 12:20pm UTC](https://www.ruby-forum.com/t/problem-with-string-gsub-and-as-a-replacement-string/120482/1 "2007-11-17T12:20:36Z")

</div>

Hi,

I want to replace all occurences of a certain character in a string  
with a backslash followed by a single quote. Sounds like a trivial  
task, but this is what I get:

“this is a test”.gsub( “a”, “\’” ) -\> “this is test test”

What I want is “this is ’ test”.

Neither does this work:  
“this is a test”.gsub( “a”, ‘’ + “’” )

No matter what I am doing, as soon as a backslash is followed by a  
single quote in the replacement string, I am getting weird results.

Thanks for your help!

Andreas

---

<div class="post-metadata">

**Author:** ![ridcully](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5f9b8f/32.png) [@ridcully](https://www.ruby-forum.com/u/ridcully)\
**Post date:** [November 17, 2007, 12:23pm UTC](https://www.ruby-forum.com/t/problem-with-string-gsub-and-as-a-replacement-string/120482/2 "2007-11-17T12:23:35Z")

</div>

ridcully wrote:

> Neither does this work:  
> “this is a test”.gsub( “a”, ‘’ + “’” )
> 
> No matter what I am doing, as soon as a backslash is followed by a  
> single quote in the replacement string, I am getting weird results.  
> Backslashes are confusing. Try this:

puts “this is a test”.gsub( “a”, “\\’” )

---

<div class="post-metadata">

**Author:** ![ridcully](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5f9b8f/32.png) [@ridcully](https://www.ruby-forum.com/u/ridcully)\
**Post date:** [November 17, 2007, 12:30pm UTC](https://www.ruby-forum.com/t/problem-with-string-gsub-and-as-a-replacement-string/120482/3 "2007-11-17T12:30:36Z")

</div>

On 17 Nov., 12:22, Alex Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Backslashes are confusing. Try this:
> 
> puts “this is a test”.gsub( “a”, “\\'” )
> 
> –  
> Alex

Thank you, you saved my day!

This really is confusing, because “\'” gives me the correct result if  
I don’t use it with gsub:

puts “\'” → '

Am I missing something here?

Andreas

---

<div class="post-metadata">

**Author:** ![ridcully](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5f9b8f/32.png) [@ridcully](https://www.ruby-forum.com/u/ridcully)\
**Post date:** [November 17, 2007, 2:06pm UTC](https://www.ruby-forum.com/t/problem-with-string-gsub-and-as-a-replacement-string/120482/4 "2007-11-17T14:06:02Z")

</div>

On Nov 17, 2007 7:20 PM, ridcully [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> “this is a test”.gsub( “a”, “\'” ) → “this is test test”

many ways, eg

~\> “this is a test”.gsub(“i”,Regexp.escape(“\'”))  
=\> “th\'s \'s a test”

~\> “this is a test”.gsub(“i”){“\'”}  
=\> “th\'s \'s a test”

the block form seems clean though.

kind regards -botp

---

<div class="post-metadata">

**Author:** ![ridcully](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5f9b8f/32.png) [@ridcully](https://www.ruby-forum.com/u/ridcully)\
**Post date:** [November 17, 2007, 12:48pm UTC](https://www.ruby-forum.com/t/problem-with-string-gsub-and-as-a-replacement-string/120482/5 "2007-11-17T12:48:44Z")

</div>

ridcully wrote:

> Am I missing something here?  
> “\’” translates to a literal backslash followed by ‘. This is what gsub  
> gets.  
> gsub then sees ’ and replaces it with $’ the same way it would replace  
> \1  
> with $1. To tell it not to do that it has to get \’, so it knows it’s  
> not  
> supposed to treat ’ as special. In order to archieve that you have to  
> write “\\’”. Hope that cleared things up for you.
