Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

216
Views
¿Cómo quitar HTML del texto usando JavaScript pero manteniendo un espacio entre cada línea?

Estoy usando esta respuesta para quitar HTML del texto.

Así es como funciona:

 function strip(html){ let doc = new DOMParser().parseFromString(html, 'text/html'); return doc.body.textContent || ""; }

Si tengo algo de HTML como este:

 This is the first line<br> This is the second line<br> This is the third line

Elimina los saltos de línea y crea un texto como este:

 This is the first lineThis is the second lineThis is the third line

Pero como puede ver cuando elimina los saltos de línea, no queda espacio entre cada oración. No es así como quiero que se vea.

¿Cómo retengo un espacio entre cada línea (u oración) después de que elimina el HTML?

Quiero que se vea algo como esto:

 This is the first line This is the second line This is the third line
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Sí, eso es posible, pero tendrías que manipular el documento antes de obtener el contenido del texto:

 let doc = new DOMParser().parseFromString('This is the first line<br>\ This is the second line<br>\ This is the third line', 'text/html'); console.log(doc.body.textContent); doc.body.querySelectorAll('br') // Get all <br> elements .forEach(br => br.after(doc.createTextNode(' '))); // And spaces after them console.log(doc.body.textContent);

Estoy usando querySelectorAll para obtener todos los saltos de línea y luego agrego un nodo de texto de espacio después de cada uno.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!