开发者

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

开发者 https://www.devze.com 2023-03-11 15:14 出处:网络
How can I change one or more line breaks to something in ruby? article.content.gsub(/\\n/, \"<br />\")

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

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

above code will change every 1 line break to <br /> tag, however, I want 开发者_JS百科to change one or more \n to <br /> tag. In that way, continuous line breaks with empty lines will be substitued into a single <br /> tag. How can I do that?


Are you looking for this?

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

Note the plus sign after the \n. That will change any sequence of one or more newlines to a single <br>.


It also might be helpful to quickly test Ruby regular expressions against sample data using Rubular.

0

精彩评论

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