Convert non-breaking spaces to spaces in Ruby

后端 未结 6 793
春和景丽
春和景丽 2020-12-15 03:56

I have cases where user-entered data from an html textarea or input is sometimes sent with \\u00a0 (non-breaking spaces) instead of spaces when encoded as utf-8

6条回答
  •  余生分开走
    2020-12-15 04:38

    Use /\u00a0/ to match non-breaking spaces. For instance s.gsub(/\u00a0/, ' ') converts all non-breaking spaces to regular spaces.

    Use /[[:space:]]/ to match all whitespace, including Unicode whitespace like non-breaking spaces. This is unlike /\s/, which matches only ASCII whitespace.

    See also: Ruby Regexp documentation

提交回复
热议问题