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

159
Vistas
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 Respuestas
Responde la pregunta

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 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