开发者

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?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜