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

147
Visualizações
Javascript substring replace and output

Hi guys I wonder how to reconstruct string. I need to replace br tag inside string with '\n' new line character. So I'm simply doing it like this:

let a='Some<br>Text'
let b=a.replace(/<br>/gi, '\n');

But when I try to make an output to console this way:

console.log(JSON.stringify(b))

It shows the string like this:

Some\nText

But if I'm doing output this way:

console.log(b)

It returns:

Some
Text

So why? And is it possible to use console.log(JSON.stringify(b)) to show the string in a proper way. I mean like this: Some Text

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Because the stringify method converts all the characters to string, so you wont see line breaks as expected. If you want to display your text on the same line, you can just replace (inside your regexp replacer) the newline '\n' with an empty space ' '

about 4 years ago · Juan Pablo Isaza Relatório

0

just replace br with an empty space instead of \n, since \n means a new line.This is why your second word starts from new line

let a='Some<br>Text'
let b=a.replace(/<br>/gi, " ");

output

Some Text
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