我想删除
之间的所有新行字符<div class="some class"> arbitrary amount of text here with possible new line characters </div>
红宝石可以吗?
答案 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>