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/", "")}
Converting non-breaking spaces to regular spaces in an encoded Ruby string
Try string.gsub("\u00A0", " ")
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
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 replace consecutive occurrences of white space in each element of my array?
Do it with simple gsub
not gsub!
words.map do |w|
#respond_to?(:gsub) if you are not sure that array only from strings
w.gsub(/(?<=[^\,\.])\s+|\A\s+/, '') if w.respond_to?(:gsub)
end
Because gsub!
can return nil
if don't change the string and then you try to do gsub!
again with nil
. That's why you get an undefined method gsub!' for nil:NilClass
error.
From gsub! explanation in ruby doc:
Performs the substitutions of String#gsub in place, returning str, or
nil if no substitutions were performed. If no block and no replacement
is given, an enumerator is returned instead.
As mentioned @CarySwoveland in comments \s
doesn't handle non-breaking spaces. To handle it you should use [[:space:]]
insted of \s
.
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
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.)
Related Topics
Can't Install Thrift Gem on Os X El Capitan
Rails - Multiple Top Level Domains and a Single Session/Cookie
Accessing Objects Memory Address in Ruby
Are Rails Controllers Multithreaded? Thread.Exclusive in Controllers
Rails How to Get Latitude and Longitude from String Address and Put It on Google Map
How to Compile Ruby with Rvm on a Low Memory System
Dynamic Rails Routes Based on Database Models
Creating a Capistrano Task That Performs Different Tasks Based on Role
Ruby To_JSON Issue with Error "Illegal/Malformed Utf-8"
Rails How to Update a Column After Saving
How to Break from Nested Loops in Ruby
How to Parse JSON Request Body in Sinatra Just Once and Expose It to All Routes
Ruby 2.0 How to Uninclude a Module Out from a Module After Including It
Cannot Execute "Rails Console" Due to an Error with Readline