Jeremy Jeremy - 1 month ago 9
Ruby Question

Ruby remove new line character between html tags.

I would like to remove all the new line characters between

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


Is this possible in ruby?

Answer

Yes, you can easily do this using the Nokogiri gem. For example:

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

When run will output this:

<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>
Comments