【问题标题】:regex to strip all html tags except span tag正则表达式去除除 span 标签外的所有 html 标签
【发布时间】:2011-05-15 20:42:02
【问题描述】:

我正在尝试从字符串中删除除

之外的所有 html

<span class="match">...text...</span>

假设一个字符串是"<p>Hello there</p><span class="match">wassup</span>"

我只需要"Hello there<span class="match">wassup</span>"

有人可以帮忙吗?我在 ruby​​ (rails) 中这样做

【问题讨论】:

  • 正则表达式不是您的答案。如果您因为稍后将在页面上显示它而尝试剥离 HTML,那么使用正则表达式最终会咬到您。 stackoverflow.com/questions/701166/…
  • 考虑<p>Hello <!-- </p> --> there</p>
  • 是的...我使用了 chris 建议的 sanitize helper...我之前使用 gsub 去除所有 html 标签...所以我想我可以使用一些正则表达式...
  • 克苏鲁问题的重复。

标签: ruby regex


【解决方案1】:

ActionView::Helpers 提供了sanitize 方法来执行此操作。

http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html#method-i-sanitize

sanitize "some html", :tags => %w"span"

【讨论】:

    猜你喜欢
    • 2023-03-10
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多