使用 Rails 3.2。我想删除其中的所有文本<b>
和标签,但我设法找到只剥离标签的方法:
string = "
<p>
<b>Section 1</b>
Everything is good.<br>
<b>Section 2</b>
All is well.
</p>"
string.strip_tags
# => "Section 1 Everthing is good. Section 2 All is well."
我想实现这个目标:
"Everthing is good. All is well."
我也应该添加正则表达式匹配吗?
“正确”的方法是使用 html 解析器,例如Nokogiri http://nokogiri.org.
然而,对于这个简单的任务,您可以使用正则表达式。这很简单:
搜索 :(?m)<b\s*>.*?<\/b\s*>
并将其替换为空字符串。之后,使用strip_tags
.
正则表达式解释:
(?m) # set the m modifier to match newlines with dots .
<b # match <b
\s* # match a whitespace zero or more times
> # match >
.*? # match anything ungreedy until </b found
<\/b # match </b
\s* # match a whitespace zero or more times
> # match >
Online demo http://rubular.com/r/7y9faY3WJj
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)