Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

249
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda