How do I remove a non-breaking space in Ruby
irb(main):001:0> d = "foo\u00A0\bar"
=> "foo \bar"
irb(main):002:0> d.gsub("\u00A0", "")
=> "foo\bar"
Convert non-breaking spaces to spaces in Ruby
For the old versions of ruby (1.8.x), the fixes are the ones described in the question.
This is fixed in the newer versions of ruby 1.9+.
Change all to normal blank space Ruby
Yes, Your changes aren't working in the first place, as you have used wrong pattern, You are supposed to add an "\" before an special character which in your case is "&".
So try this:
courses = Course.all
courses.each {|course| course.shortname.gsub!(/\ /," ")}
 
is same as ASCII code 160
, so
courses = Course.all
courses.each {|course| course.shortname.gsub!(160.chr("UTF-8")," ")}
Also while trying i noticed the   is same as "\u00A0" as i got an error as undefined method `html' for "\u00A0":String. I don't know what it is but this might also works,
courses = Course.all
courses.each {|course| course.shortname.gsub!("/\u00a0/", "")}
Non-breaking space between lines
How did you use succeed? Where did the space end up?
Did you combine it with the whitespace removal chars ("<"
and ">"
) or try them on their own?
= succeed ' '.html_safe do
= 'foo'
%a(href='http://example.com')> bar
This produces:
foo <a href='http://example.com'>bar</a>
Is that literally what you want? It is "two lines joined by a non-breaking space with no actual space."
(IMO, HAML is meh for inline content, and just no fun. Nice for layout.)
Ruby 1.9 strip not removing whitespace
I think, there are a lot of "space characters".
You can use something like this:
my_string.gsub("\302\240", ' ').strip
How to remove whitespace from string in Ruby
It looks like you have some non-breaking space characters in your file (C2 A0
is the UTF-8 encoding for U+00A0, which is non breaking space).
In Ruby’s regular expressions \s
doesn’t match non-breaking spaces, which is why your code doesn’t appear to work (strip
also doesn’t remove them). You can use the \p{Space}
character property instead, or the POSIX bracket expression [[:space:]]
.
day_to = day_to.gsub(/\p{Space}/,'') unless day_to.nil?
or
day_to = day_to.gsub(/[[:space:]]/,'') unless day_to.nil?
Ruby regexp handling of nbsp
Use Unicode properties (you need to declare a matching source code encoding for this to work):
# encoding=utf-8
if subject ~= /\p{Z}/
# subject contains whitespace or other separators
or use POSIX character classes:
if subject ~= /[[:space:]]/
According to the docs, \s
will only match [ \t\r\n\f]
now and in the future.
How do I split a string on non-traditional white-space?
I suggest using
/[ \u00A0]*(?:[[:space:]&&[^ \u00A0]][ \u00A0]*)+|[[:space:]]{2,}/
See the regex demo.
Although it seems \s
can match any Unicode whitespace if the pattern is prepended with (?u)
, [[:space:]]
seems to be a more adopted way to match any Unicode whitespace.
Details:
The pattern has 2 branches that match either...
[ \u00A0]*(?:[[:space:]&&[^ \u00A0]][ \u00A0]*)+
- a chunk of whitespaces that contain at least 1 non-space/non-nonbreaking space[ \u00A0]*
- zero or more occurrences of a regular or non-breaking spaces(?:[[:space:]&&[^ \u00A0]][ \u00A0]*)+
- one or more occurrences of:[[:space:]&&[^ \u00A0]]
- (a character class subtraction) any whitespace with the exception of a regular/non-breaking whitespace[ \u00A0]*
- zero or more regular/non-breaking whitespaces
|
- or[[:space:]]{2,}
- 2 or more any whitespace symbols
Related Topics
Refactoring Activerecord Models with a Base Class Versus a Base Module
Use Separate Authentication Model with Devise on Rails
Ruby, Tor and Net::Http::Proxy
Ruby - Difference Between :Variable and @Variable
Rails Pass Params/Arguments to Activerecord Callback Function
Including a Virtual Attribute in the Respond_With Hash
Can't Install Rmagick Gem on Ubuntu 13.04
Datetime with Mongodb/Mongoid and Rails 3 Not Populating
Parsing JSON Without Quoted Keys
How to Transfer Files Using Ssh and Scp Using Ruby Calls
How to Timeout Flash Messages in Rails
How to Detect Browser Type and Its Version
What Does "No Binary Rubies Available" Mean
How to Pluck Email from Array of Users
How to Disable a Form Submit Button "A Là Ruby on Rails Way"