How to replace multiple newlines in a row with one newline using Ruby

后端 未结 9 853
陌清茗
陌清茗 2021-02-05 03:42

I have a script written in ruby. I need to remove any duplicate newlines (e.g.)

\\n
\\n
\\n

to

\\n

My current

9条回答
  •  被撕碎了的回忆
    2021-02-05 04:04

    Simply calling split will also trim out all of your whitespace.

    You need to pass \n to split

    >> "one   ok \ntwo\n\nthree\n".split(/\n+/).join("\n")
    => "one  ok \ntwo\nthree"
    

提交回复
热议问题