如何在 ruby 中删除空格但不删除 utf-8 字符
我想阻止用户编写空注释(空格、
等)。所以我应用了以下内容:
var.gsub(/^\s+|\s+\z|\s* \s*/.'')
但是,聪明的用户通过使用 \302
或 \240
unicode 字符发现了一个漏洞,所以我也过滤掉了这些字符。
然后我遇到了问题,因为我引入了多种语言支持,然后像 Déjà vu
这样的词就变成了错误。因为 à
字符的一部分包含 \240
。有什么方法可以删除空格但保持拉丁字符不变?
I want to prevent users to write an empty comment (whitespaces,
, etc.). so I apply the following:
var.gsub(/^\s+|\s+\z|\s* \s*/.'')
However, then a smart user find a hole by using \302
or \240
unicode characters so I filtered out these characters too.
Then I ran into problem as I introduced several languages support, then a word like Déjà vu
becomes an error. because part of the à
character contains \240
. is there any way to remove the whitespaces but leave the latin characters untouched?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决这个问题的方法是在使用正则表达式删除空格之前使用
iconv
丢弃无效的 unicode 字符(例如\230
本身):A way around this is to use
iconv
to discard the invalid unicode characters (such as\230
on its own) before using your regexp to remove the whitespaces: