gsub HELP

N

Newb Newb

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...
 
N

Newb Newb

Newb said:
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=".*".*?\/\>/,' ')
 
R

Robert Dober

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
 
N

Newb Newb

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

thanks a lot
it works
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
DewittMill
Top