matt-rock matt-rock - 6 months ago 37
Ruby Question

Nokogiri remove White Space

how do remove spaces in my Code. If get the following Code parse with Nokogiri, I get the following output

<div class="address-thoroughfare mobile-inline-comma ng-binding">Kühlungsborner Straße
10
</div>


Output:

Kühlungsborner Straße
10


This Output is not left-justified.

My Nokogiri Code:

address_street = page_detail.xpath('//div[@class="address-thoroughfare mobile-inline-comma ng-binding"]').text

Answer

strip only removes ASCII whitespace and the output you are getitng here is a Unicode no-break space.

You can use gsub by providing a regex with the character code: address_street.gsub(/\u00a0/, ' ')

You can also use address_street.gsub(/[[:space:]]/, ' ') to remove all Unicode whitespace.

remove whitespace from nokogiri output