Ruby删除html标记之间的新行字符。

时间:2016-10-09 17:29:17

标签: ruby regex replace find

我想删除

之间的所有新行字符
<div class="some class"> arbitrary amount of text here with possible new line characters </div>

红宝石可以吗?

1 个答案:

答案 0 :(得分:1)

是的,您可以使用Nokogiri宝石轻松完成此操作。例如:

require "rubygems"
require "nokogiri"

html = %q!
<div class="some class">      arbitrary amount of text
here with possible
new line
characters        </div>
!


doc = Nokogiri::HTML::DocumentFragment.parse(html)
div = doc.at('div')
div.inner_html = div.inner_html.gsub(/[\n\r]/, " ").strip

puts html
puts '-' * 60
puts doc.to_s

运行时会输出:

<div class="some class">      arbitrary amount of text
here with possible
new line
characters        </div>
------------------------------------------------------------

<div class="some class">arbitrary amount of text  here with possible  new line  characters</div>