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

发布于 2024-12-21 18:18:49 字数 440 浏览 2 评论 0原文

我正在尝试用 HTMl 文档中的文本替换

标记。我开始使用 Nokogiri 来做到这一点。问题是我传递给 Nokogiri 的 HTML 包含类似 <[email protected] 的文本]> nokogiri 将其视为标签。输出全部搞砸了,因为 Nokogiri 尝试通过将 添加到 html 文档来更正“标签”。

有什么想法如何在 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 <[email protected]> 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半﹌身腐败 2024-12-28 18:18:49

在将字符串发送到 Nokogiri 之前,您可以对邮件周围的 <> 进行转义。

>> "<html><body><[email protected]></body></html>".gsub(/<([^<]+@[^>]+)>/,'<\1>')
=> "<html><body><[email protected]></body></html>"

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

>> "<html><body><[email protected]></body></html>".gsub(/<([^<]+@[^>]+)>/,'<\1>')
=> "<html><body><[email protected]></body></html>"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文