简体   繁体   中英

gsub string for all spaces in Ruby 1.8

I have a string with spaces (one simple space and one ideographic space):

"qwe rty uiop".gsub(/[\s]+/,'')    #=> "qwe rtyuiop"

How can I add all space-codes (for example 3000, 2060, 205f) in my pattern?

In Ruby 1.9 I just added \  and other codes, but how do it in 1.8?

I think i found answer. In ActiveSupport::Multibyte::Chars is a UNOCODE_WHITESPACE constant. Solution:

pattern = ActiveSupport::Multibyte::Chars::UNICODE_WHITESPACE.collect do |c|
  c.pack "U*"
end.join '|'
puts "qwe rty uiop".mb_chars.gsub(/#{pattern}/,'')
#=> qwertyuiop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM