Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
How to remove line break if it is repeated more than 2 times?

Input: "Some text \n\n\nSome text"

Output: "Some text \n\nSome text"

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You could use gsub to replace 3 or more \n's with \n\n:

str = "Some text \n\n\nSome text"

str.gsub(/\n{3,}/, "\n\n")
#=> "Some text \n\nSome text"

or you could use a positive lookbehind to match 1 or more \n that are preceded by \n\n and remove the excess \n's:

str.gsub(/(?<=\n\n)\n+/, '')
#=> "Some text \n\nSome text"
over 4 years ago · Santiago Trujillo Report

0

my_string = 'Some text \n\n\n\n\nSome text';
while my_string.count('\n') > 2
   my_string = my_string.reverse.sub('\n', '').reverse;
end

puts my_string;
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!