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

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

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 Denunciar

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