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

139
Visualizações
parse html tags from string and make an array in javaScript, vue.js

I am trying to make an array from string. the string contains HTML tags like P, span, a,img. some are nested like span tag inside p, img tag inside a etc. i would not replace any tags or styles. I am trying to parse all the tags and make an array.

Example code looks like this in textData variable

var line_array =[];
var textData : `
     <img src="imgurl" alt="" /> 
     <p>some text</p>
     <p style="cssStyle">some text</p>
     <span style="cssStyle"> some text </span>
     <p style="cssStyle">some text<span style="cssStyle"> span text </span></p>
     <a href=""><span style="style">text</span><img src="https://via.placeholder.com/100" alt=""/>  </a>
`
activate: function (){
      let sourceText = document.getElementById('sourceText').innerHTML;
      line_array = sourceText;
}

I am trying to make an array look like this... from textData

line_array =[
'<img src="imgurl" alt="" />', 
'<p>some text</p>', 
'<p style="cssStyle">some text</p>',
'<span style="cssStyle"> some text </span>',
'<p style="cssStyle">some text<span style="cssStyle"> span text </span</p>',
'<a href=""><span style="style">text</span><img src="imgurl" alt=""/></a>'
]
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This is hard or impossible to do with regexp alone (depending on the complexity of HTML that you want/need to allow). You can use split-html.

    <script src="https://cdn.jsdelivr.net/npm/split-html@1.1.0/index.min.js"></script>
    <script>
        const textData = `
               <img src="imgurl" alt="" /> 
               <p>some text</p>
               <p style="cssStyle">some text</p>
               <span style="cssStyle"> some text </span>
               <p style="cssStyle">some text<span style="cssStyle"> span text 
               </span></p>
                <a href=""><span style="style">text</span><img src="https://via.placeholder.com/100" alt=""/>  </a>
        `
        const line_array = splitHtml(textData, 'img, a, p').filter(String);
        console.log(line_array);
    </script>
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