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

257
Vistas
How to split multiline string into lines?

How to split a multiline string into lines without taking into account the newline in the line itself?

My approach is not working:

const str = `
Hello
World\t
its\nbeautiful
`;

JSON.stringify(str).split(/$\\n/g)

What is the result of this approach:

[""", "Hello", "World\t", "its", "beautiful", """]

What result is needed in the end:

[""", "Hello", "World\t", "its\nbeautiful"]
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Since \n is the character that marks new lines, just like a normal new line, there is no way for javascript to differenciate between \n and \n.

An idea for your "special" new lines could be to escape that \n with another \ so you would end up with

const str = `
Hello
World\t
its\\nbeautiful
`;
str.split("\n");

The result would be this:

['', 'Hello', 'World\t', 'its\\nbeautiful', '']
about 4 years ago · Juan Pablo Isaza 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