Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ruby and Nokogiri: Removing trailing spaces

Ruby and Nokogiri: Removing trailing spaces

sample_input = "<div>Hi&nbsp; &nbsp; &nbsp;</div>"
parsed_input = Nokogiri::HTML.parse(sample_input).text #=> "Hi   "

parsed_input.gsub(/\p{Space}*\z/, "") #=> "Hi"

\p{Space} catches any whitespace character.

Source: https://stackoverflow.com/questions/22645916/unable-to-strip-a-stubborn-space-from-a-ruby-string-nokogiri-is-involved#comment34491990_22646219

Regexp Character Properties: https://ruby-doc.org/core-2.5.0/Regexp.html#class-Regexp-label-Character+Properties

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.