Jeremy
Jeremy

Reputation: 675

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?

Upvotes: 1

Views: 474

Answers (1)

Philip Hallstrom
Philip Hallstrom

Reputation: 19879

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>

Upvotes: 3

Related Questions