sample_input = "<div>Hi </div>"
parsed_input = Nokogiri::HTML.parse(sample_input).text #=> "Hi "
parsed_input.gsub(/\p{Space}*\z/, "") #=> "Hi"
\p{Space}
catches any whitespace character.
Regexp Character Properties: https://ruby-doc.org/core-2.5.0/Regexp.html#class-Regexp-label-Character+Properties