[英]How do you replace with a newline using gsub?
I have the following: 我有以下几点:
str = str.gsub(re, '<pre><code>\1</code></pre>'
Which outputs: 哪个输出:
<pre><code> stuff...
I would like for <pre><code>
to be on its on line by itself. 我希望<pre><code>
自己在线。 How can I insert a newline character there? 如何在其中插入换行符? If I enter \\n
it shows up as HTML. 如果输入\\n
它将显示为HTML。
Newline characters need to be enclosed by double quotes. 换行符必须用双引号引起来。 With double quotes you'll need an extra backslash for the pattern match: 使用双引号将需要为模式匹配额外加一个反斜杠:
str = str.gsub(re, "<pre><code>\n\\1\n</code></pre>"
String concatenation can help simplify the problem and avoid the leaning toothpick syndrome: 字符串串联可以帮助简化问题并避免牙签倾斜综合征:
str = str.gsub(re, '<pre><code>\1</code></pre>'
Becomes: 成为:
str = str.gsub(re, "\n<pre><code>\n" + '\1' + "\n</code></pre>\n"
You can see what it's doing in IRB: 您可以看到它在IRB中的功能:
"\n<pre><code>\n" + '\1' + "\n</pre></code>\n"
=> "\n<pre><code>\n\\1\n</pre></code>\n"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.