用 BR 替换 unicode 换行符
在我的 XML 文件中,有像此屏幕截图所示的 unicode 换行符。 使用此链接查看屏幕截图
“minds”后面的两个点。是换行符。我用谷歌搜索并尝试了几乎所有我知道的东西来用 ruby (1.8) 替换它们,但没有任何运气。
这是我的代码(使用不同的 unicode 尝试),也许有人可以帮助我。
def formatedBody
t = self.body.gsub("\u000a","<br/>")
t = t.gsub("\u000d","<br/>")
t = t.gsub("\u0009","<br/>")
t = t.gsub("\u000c","<br/>")
t = t.gsub("\u0085","<br/>")
t = t.gsub("\u2028","<br/>")
t = t.gsub("\u2029","<br/>")
t = t.gsub(/0A\0A/u,"<br/>")
return t
end
In my XML files, there are unicode line breaks like shown in this screenshot.
Use this link to see the screenshot
The two dots after "minds." is the line break. I've googled and tried almost everything I know to replace them with ruby (1.8) but without any luck.
Here's my code (with different tries of unicodes), maybe someone could help me.
def formatedBody
t = self.body.gsub("\u000a","<br/>")
t = t.gsub("\u000d","<br/>")
t = t.gsub("\u0009","<br/>")
t = t.gsub("\u000c","<br/>")
t = t.gsub("\u0085","<br/>")
t = t.gsub("\u2028","<br/>")
t = t.gsub("\u2029","<br/>")
t = t.gsub(/0A\0A/u,"<br/>")
return t
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
两个 0x0A 值是换行符的十六进制表示。常规 ol' ASCII 换行符,又称字符串中的
"\n\n"
。因此,
t = t.gsub(/\n/, "
应该可以工作。")
您可以将 OR 字符列表替换为:
无论哪种方式,输出都会如下所示:
The Reason your
does not work is the regex is not valid.
是定义的另一种方式:
The two
0x0A
values are the hex representation of line-feeds. Regular ol' ASCII line feeds, AKA"\n\n"
in a string.So,
t = t.gsub(/\n/, "<br/>")
should work.You can replace the list of OR'd characters with:
Either way, the output would look like:
The reason your
doesn't work is the regex is not correct.
is an alternate way of defining:
我遇到了同样的问题(使用 ruby 1.8.7),我只是用以下方法解决了它:
I had the same issue (using ruby 1.8.7) and I simply solve it with: