# Gsub HELP

**URL:** <https://www.ruby-forum.com/t/gsub-help/146627>\
**Category:** Ruby\
**Created:** [August 23, 2008, 6:56am UTC](https://www.ruby-forum.com/t/gsub-help/146627 "2008-08-23T06:56:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [August 23, 2008, 6:56am UTC](https://www.ruby-forum.com/t/gsub-help/146627/1 "2008-08-23T06:56:52Z")

</div>

Hi all  
I am not Expert at Rails or Ruby  
I have a string …it contains text and images also…  
from that string i want to replace the images into empty space…that  
is images Has to Be Removed.  
I tried to solve By using string.gsub…But doesn’t Work  
I tried like below

item.description = item.description.gsub("img src ",’’)  
So any guru can give Me solution…

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [August 23, 2008, 9:10am UTC](https://www.ruby-forum.com/t/gsub-help/146627/2 "2008-08-23T09:10:19Z")

</div>

Newb N. wrote:

> Hi all  
> I am not Expert at Rails or Ruby  
> I have a string …it contains text and images also…  
> from that string i want to replace the All The Images Into  
> Empty\>space…that is images Has to Be Removed.
> 
> I tried like below
> 
> item.description = item.description.gsub(/\<_img.src="."._?/\>/,’ ')

> ```
> Using This Regular Expression I can able to Reomve Only The First
> 
> ```
> 
> Occurrences of The Images,But I Need TO Remove All Occurences Of The Images

> So Any Guru Can give Me solution…

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [August 23, 2008, 11:11am UTC](https://www.ruby-forum.com/t/gsub-help/146627/3 "2008-08-23T11:11:13Z")

</div>

On Sat, Aug 23, 2008 at 9:06 AM, Newb N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Newb N. wrote:
> 
> > Hi all  
> > I am not Expert at Rails or Ruby  
> > I have a string …it contains text and images also…  
> > from that string i want to replace the All The Images Into  
> > Empty\>space…that is images Has to Be Removed.
> > 
> > I tried like below
> > 
> > item.description = item.description.gsub(/\<_img.src="."._?/\>/,’ ')  
> > The . stays for any character, as such your regular expression seems  
> > to be way too greedy.  
> > This however should not harm too much if there is only one img tag in  
> > one line, although backtracking  
> > might be a performance issue. I do however fail to understand the  
> > behavior you described.  
> > Anyway, I believe the following regex should do fine:

item.description.gsub! /\<img.\*?\>/, “”

HTH  
Robert

–  
[http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

There’s no one thing that’s true. It’s all true.

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [August 23, 2008, 11:47am UTC](https://www.ruby-forum.com/t/gsub-help/146627/4 "2008-08-23T11:47:27Z")

</div>

> The . stays for any character, as such your regular expression seems  
> to be way too greedy.  
> This however should not harm too much if there is only one img tag in  
> one line, although backtracking  
> might be a performance issue. I do however fail to understand the  
> behavior you described.  
> Anyway, I believe the following regex should do fine:
> 
> item.description.gsub! /\<img.\*?\>/, “”
> 
> HTH  
> Robert
> 
> –  
> [http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)
> 
> There’s no one thing that’s true. It’s all true.

thanks a lot  
it works

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [August 23, 2008, 4:16pm UTC](https://www.ruby-forum.com/t/gsub-help/146627/5 "2008-08-23T16:16:09Z")

</div>

> > item.description = item.description.gsub(/\<_img.src="."._?/\>/,’ ')
> 
> > ```
> > Using This Regular Expression I can able to Reomve Only The First
> > 
> > ```
> > 
> > Occurrences of The Images,But I Need TO Remove All Occurences Of The Images

How did you do with the \>10 replies you got before? Did Hpricot work  
out?
