How do I remove a node with Nokogiri?

all jazz picture all jazz · Nov 10, 2009 · Viewed 29.9k times · Source

How can I remove <img> tags using Nokogiri?

I have the following code but it wont work:

# str = '<img src="canadascapital.gc.ca/data/2/rec_imgs/5005_Pepsi_H1NB.gif"/…; testt<a href="#">test</a>tfbu' 

f = Nokogiri::XML.fragment(str)
f.search('//img').each do |node| 
  node.remove
end
puts f

Answer

xds2000 picture xds2000 · Nov 12, 2009

have a try!

f = Nokogiri::XML.fragment(str)

f.search('.//img').remove
puts f