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

164
Visualizações
Regex match just first result or second from HTML table

I'm using regex in-app script javascript. I try to get just the first value or the second value from each table but not work.

   <table>
      <td class="bar">
        <td class="center">hello1</td>
        <td class="center">hello2</td>
        <td class="center">hello3</td>
     </td>
   </table>

   <table>
     <td class="bar">
       <td class="center">hello1</td>
       <td class="center">hello2</td>
       <td class="center">hello3</td>
     </td>
   </table>

my code regex: /(?<=<td class=\"center\">).*?(?=<\/td>)/gi

this code selects all values Hello. I want just select Hello1 from the first table td and second table td. I remove global but give me just the first td from the first table.

the hello value is dynamic not fixed.

enter image description here

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

You could do a regular expression something like this which retrieves the text you expect...

regular expression run results

Essentially matching the tags by excluding <> characters within the tag.

However I think an easier and less error prone way to do this in JavaScript and DOM is to use querySelectorAll instead, for example...

let c = document.querySelectorAll('td:nth-of-type(2)');

console.log(Array.from(c).map(i => i.innerText));
   <table>
      <td class="bar">
        <td class="center">hello1</td>
        <td class="center">hello2</td>
        <td class="center">hello3</td>
     </td>
   </table>

   <table>
     <td class="bar">
       <td class="center">hello1</td>
       <td class="center">hello2</td>
       <td class="center">hello3</td>
     </td>
   </table>

about 4 years ago · Santiago Gelvez 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