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

235
Vistas
Dividir cadena con marcado HTML en objetos separados usando JS

Estoy tratando de dividir diferentes partes de una cadena en nuevos objetos dependiendo de si hay marcado HTML. Esto es lo que probé, también probé con split() , sin embargo, no puedo encontrar una manera de extraer el contenido dentro de las marcas HTML.

 const example = 'The lucky stars is an astrology themed <a href="/game/">game</a> and it consists of <strong>thousands of.</strong>' const richTexts = []; example.match(/\<.*?\>/g).forEach((e) => { if (e.includes('<a')) { richTexts.push({ type: 'hyperlink', content: '' }) } else if (e.includes('<strong') { richTexts.push({ type: 'bold', content: '' }) } else { richTexts.push({ type: 'text', content: '' }) } }) console.log(richTexts);

La salida deseada es:

 [ { type: 'text', content: 'The lucky stars is an astrology themed ' }, { type: 'hyperlink', content: 'game' }, { type: 'text', content: ' and it consists of ' }, { type: 'bold', content: 'thousands of.' } ]
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

No es elegante, pero resuelve tu ejemplo:

 const example = 'The lucky stars is an astrology themed <a href="/game/">game</a> and it consists of <strong>thousands of.</strong>' const richTexts = []; const res = example.split('<') res.forEach(r => { let obj = {} if(r.includes('>')) { if(r.includes('strong')){ obj.type= "bold" } else if (r.includes('href')) { obj.type = "hyperlink" } else { obj.type = 'text' } } else { obj.type = 'text' } r = r.slice(r.indexOf('>') + 1, r.length) if(r.length) { obj.content = r richTexts.push(obj) } }) console.log(richTexts)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Mi sugerencia:

 const example = 'The lucky stars is an astrology themed <a href="/game/">game</a> and it consists of <strong>thousands of.</strong>'; const richTexts = []; var strType; example.split(/((?:[^\s<]*<\w[^>]*>[\s\S]*?<\/\w[^>]*>)+[^\s<]*)/).filter(Boolean).forEach(e => { strType = 'text'; if (e.includes('<a')) { strType = 'hyperlink' } else if (e.includes('<strong')) { strType = 'bold' }; richTexts.push({ type: strType, content: strType === 'text' ? e : e.replace(/(<([^>]+)>)/g, '') }); }); console.log(richTexts);

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