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

84
Visualizações
How to ignore tag inside RegExp

In my project, we use a RegExp to display the title of cards that we receive from the deck. And recently I found that from the deck side we sometimes receive different formats and the titles didn't display.

So, before it was always a string like this:

const res =
  `<p class="cardTitle" style="font-size: 27.2px">
    <span>Some text here</span><span> - Title</span>
   </p>`;

and the RegExp was:

/<p class="cardTitle"[^>]*>[\s]*<span[^>]*>(.+)<\/span><span[^>]*>(.+)<\/span><div[^>]*>(.+)<\/div>+[\s]*<\/p>/i.exec(res);

Now sometimes we receive res with div and <br> tags inside

const res = 
  `<p class="cardTitle" style="font-size: 27.2px">
    <span>Some text here</span><span> - Title</span>
    <div style="font-size: 10px">Title:<br>Some text here</div>
   </p>`;

The question is, how to change the RegEx to ignore this <div>..<br>.</div>?

Here's a demo:

const res =
  `<p class="cardTitle" style="font-size: 27.2px">
    <span>Some text here</span><span> - Title</span>
   </p>`;
   
const newRes =
  `<p class="cardTitle" style="font-size: 27.2px">
    <span>Some text here</span><span> - Title</span>
    <div style="font-size: 10px">Title:<br>Some text here</div>
   </p>`;
   
const regEx = /<p class="cardTitle"[^>]*>[\s]*<span[^>]*>(.+)<\/span><span[^>]*>(.+)<\/span>+[\s]*<\/p>/i;
   
const correct = regEx.exec(res);
const broken = regEx.exec(newRes);
 
console.log('correct', correct);
console.log('broken', broken);

Would be really grateful for any help!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Simplify the regex

/<p class="cardTitle"[^>]*>\s*<span[^>]*>(.*?)<\/span><span[^>]*>(.*?)<\/span>.*?<\/p>/si

This will get the p tag, with the 2 spans and whatever else it contains.

const res =
  `<p class="cardTitle" style="font-size: 27.2px">
    <span>Some text here</span><span> - Title</span>
   </p>`;
   
const newRes =
  `<p class="cardTitle" style="font-size: 27.2px">
    <span>Some text here</span><span> - Title</span>
    <div style="font-size: 10px">Title:<br>Some text here</div>
   </p>`;
   
const regEx = /<p class="cardTitle"[^>]*>\s*<span[^>]*>(.*?)<\/span><span[^>]*>(.*?)<\/span>.*?<\/p>/si;
   
const correct = regEx.exec(res);
const broken = regEx.exec(newRes);
 
console.log('correct', correct);
console.log('broken', broken);

about 4 years ago · Juan Pablo Isaza Relatório

0

Parse the htmlString into the DOM, then extract the text.

const res =
  `<p class="cardTitle" style="font-size: 27.2px">
    <span>Some text here</span><span> - Title</span>
    <div style="font-size: 10px">Title:<br>Some text here</div>
   </p>`;
const getNodes = str => {
  document.body.insertAdjacentHTML('beforeEnd', str);
  const DOM = document.querySelector('.cardTitle');
  return DOM.innerText;
};

console.log(getNodes(res));

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