简体   繁体   中英

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 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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