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

165
Views
Cómo extraer texto de una cadena html

Estoy tratando de extraer texto de una cadena html pero no funciona como se esperaba.

La cadena html que tengo es,

 <div data-content-type="html" data-appearance="default" data-element="main">&lt;p&gt;The Angelina Tank Dress is simple yet sophisticated. This dress can be thrown over a swimsuit for last minute lunch plans or belted for dinner on the patio. The high-low hemline gives it the perfect amount of swing. &lt;/p&gt;&lt;p&gt;Features:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Scoopneck&lt;/li&gt;&lt;li&gt;Sleeveless&lt;/li&gt;&lt;li&gt;Hits below the knee&lt;/li&gt;&lt;li&gt;Longer back hemline&lt;/li&gt;&lt;li&gt;Machine wash, tumble dry low&lt;/li&gt;&lt;/ul&gt;</div>

Hay un texto de descripción y texto dentro de los elementos ul li. ¿Cómo podría extraer todo ese texto por separado? Por ejemplo, extraiga el texto de la descripción por separado y el texto dentro de los elementos li por separado.

Lo intenté

 const productDescription = productDetails.description.replace(/<div>|<\/div>|<ul>|<li>/g, "").trim().split("Features:");

Me gustaría que el texto fuera

El vestido sin mangas Angelina es simple pero sofisticado. Este vestido se puede poner sobre un traje de baño para planes de almuerzo de última hora o ceñido con cinturón para cenar en el patio. El dobladillo alto y bajo le da la cantidad perfecta de swing.

Escote redondo Sin mangas Hasta debajo de la rodilla Dobladillo trasero más largo Lavar a máquina, secar en secadora a temperatura baja

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

  1. Obtener el contenido de texto del elemento.
  2. Cree un nuevo elemento div temporal y agregue el contenido de texto como HTML a ese elemento.
  3. Utilice querySelectorAll para buscar todos los elementos p y li .
  4. Iterar sobre esa lista de nodos (después de crear una matriz a partir de ella para que podamos usar map ).
  5. Para cada nodo, verifique su nombre de nodo y devuelva un objeto útil con propiedades de type y text .

 const html = document.querySelector('div').textContent; const div = document.createElement('div'); div.innerHTML = html; const els = div.querySelectorAll('p, li'); const arr = Array.from(els).map(el => { const type = el.nodeName === 'P' ? 'para' : 'item'; return { type, text: el.textContent } }); console.log(arr);
 <div data-content-type="html" data-appearance="default" data-element="main">&lt;p&gt;The Angelina Tank Dress is simple yet sophisticated. This dress can be thrown over a swimsuit for last minute lunch plans or belted for dinner on the patio. The high-low hemline gives it the perfect amount of swing. &lt;/p&gt;&lt;p&gt;Features:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Scoopneck&lt;/li&gt;&lt;li&gt;Sleeveless&lt;/li&gt;&lt;li&gt;Hits below the knee&lt;/li&gt;&lt;li&gt;Longer back hemline&lt;/li&gt;&lt;li&gt;Machine wash, tumble dry low&lt;/li&gt;&lt;/ul&gt;</div>

about 4 years ago · Juan Pablo Isaza Report

0

<script> function stripHtml(html) { var textarea = document.createElement("textarea"); textarea.innerHTML = html; var temporalDivElement = document.createElement("p"); temporalDivElement.innerHTML = textarea.value; return temporalDivElement.textContent || temporalDivElement.innerText || ""; } var htmlString = `<div data-content-type="html" data-appearance="default " data-element="main">&lt;p&gt;The Angelina Tank Dress is simple yet sophisticated. This dress can be thrown over a swimsuit for last minute lunch plans or belted for dinner on the patio. The high-low hemline gives it the perfect amount of swing. &lt;/p&gt;&lt;p&gt;Features:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Scoopneck&lt;/li&gt;&lt;li&gt;Sleeveless&lt;/li&gt;&lt;li&gt;Hits below the knee&lt;/li&gt;&lt;li&gt;Longer back hemline&lt;/li&gt;&lt;li&gt;Machine wash, tumble dry low&lt;/li&gt;&lt;/ul&gt;</div>`; console.log(stripHtml(htmlString)); </script>
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!