开发者

Ruby - Making a newline within usage of gsub

开发者 https://www.devze.com 2023-01-22 21:06 出处:网络
I\'m a bit stuck on this issue. I\'m trying to make a newline using \'\\n\'. I\'m opening a file, then replacing the text, then writing i开发者_运维技巧t back as an html file:

I'm a bit stuck on this issue. I'm trying to make a newline using '\n'. I'm opening a file, then replacing the text, then writing i开发者_运维技巧t back as an html file:

replace = text.gsub(/aaa/, 'aaa\nbbb')

But this results in:
aaa\nbbb

I'm trying to make do:
aaa
bbb


In single-quoted strings a backslash is just a backslash (except if it precedes another backslash or a quote). Use double quotes: "aaa\nbbb" .


You'll want to read:Backslashes in Single quoted strings vs. Double quoted strings in Ruby?.

0

精彩评论

暂无评论...
验证码 换一张
取 消