简体   繁体   中英

How to replace html tag in ruby without using Nokogiri?

I am trying to replace a <blockquote> tag with text in an HTMl document. I started off using Nokogiri to do this. The problem is the HTML I pass to Nokogiri contains text like <my_mail@gmail.com> which nokogiri considers as a tag. The output is all screwed because Nokogiri tries to correct the "tag" by adding </my_mail> to the html doc.

Any ideas how to turn off this auto correction in Nokogiri or are there any other gems I can use to do this without screwing the output?

You can escape < and > around mail before sending string to Nokogiri.

>> "<html><body><my_mail@gmail.com></body></html>".gsub(/<([^<]+@[^>]+)>/,'&lt;\1&gt;')
=> "<html><body>&lt;my_mail@gmail.com&gt;</body></html>"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM