【发布时间】:2018-06-13 15:55:58
【问题描述】:
我有一个带有一些 HTML 标记的字符串,但没有任何段落。我想用段落标签替换换行符,但如果我这样做是“愚蠢的方式”:
html.gsub!(/\s*\n+\s*/, '</p><p>')
html = '<p>' + html + '</p>'
,然后我会得到标记重叠:
This is text. <b>Bold text.\n
Still bold,</b> now plain.
变成:
<p>This is text. <b>Bold text.</p><p>Still bold,</b> now plain.</p>
我知道 HTML5 允许重叠标签,但它仍然非常非常难看。我想得到这样的东西:
<p>This is text. <b>Bold text.</b></p><p><b>Still bold,</b> now plain.</p>
我该如何解决这个问题?
UPD。 <br/> 不是我需要的 - 我打算使用带有自定义解析器的 Kramdown 来从这个字符串生成 Markdown,并且在字符串中我需要每个段落的打开和关闭标记。
【问题讨论】:
-
谢谢,但我不打算用 Regex 解析 HTML - 事实上,我明确称它为“愚蠢的方式”=-)
标签: html ruby-on-rails ruby