简体   繁体   English

如何在不使用Nokogiri的情况下替换ruby中的html标签?

[英]How to replace html tag in ruby without using Nokogiri?

I am trying to replace a <blockquote> tag with text in an HTMl document. 我正在尝试用HTMl文档中的文本替换<blockquote>标记。 I started off using Nokogiri to do this. 我开始使用Nokogiri来做这件事。 The problem is the HTML I pass to Nokogiri contains text like <my_mail@gmail.com> which nokogiri considers as a tag. 问题是我传递给Nokogiri的HTML包含像<my_mail@gmail.com>这样的文本,nokogiri将其视为标记。 The output is all screwed because Nokogiri tries to correct the "tag" by adding </my_mail> to the html doc. 输出全部搞砸了,因为Nokogiri试图通过将</my_mail>添加到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? 任何想法如何关闭Nokogiri中的这种自动校正或者是否有任何其他宝石可以用来做这个而不拧紧输出?

You can escape < and > around mail before sending string to Nokogiri. 在向Nokogiri发送字符串之前,您可以在邮件中转义<>

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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