Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

154
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda