简体   繁体   English

如何在 ruby 中更改一个或多个换行符?

[英]How can I change one or more line breaks to something in ruby?

How can I change one or more line breaks to something in ruby?如何在 ruby 中更改一个或多个换行符?

article.content.gsub(/\n/, "<br />")

above code will change every 1 line break to <br /> tag, however, I want to change one or more \n to <br /> tag.上面的代码将每 1 个换行符更改为<br />标签,但是,我想将一个或多个 \n 更改为<br />标签。 In that way, continuous line breaks with empty lines will be substitued into a single <br /> tag.这样,带有空行的连续换行符将被替换为单个<br />标记。 How can I do that?我怎样才能做到这一点?

Are you looking for this?你在找这个吗?

article.content.gsub(/\n+/, "<br />")

Note the plus sign after the \n .注意\n后面的加号。 That will change any sequence of one or more newlines to a single <br> .这会将一个或多个换行符的任何序列更改为单个<br>

It also might be helpful to quickly test Ruby regular expressions against sample data using Rubular .使用Rubular针对样本数据快速测试 Ruby 正则表达式也可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM