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

240
Visualizações
Regular Expression to exclude a certain pattern

I am trying to find the count of number of occurrences of a substring in a string using

function countOccurences(str,word){
   var regex = new RegExp("\\b"+word+"\\b","gi");
    console.log((str.match(regex)|| []).length);
}


let String=' test TEST TESTING Test I like testing <h3>TEST</h3> class="test id="test" ';

let asset="Test";
countOccurences(String,asset);

Here, the result I am getting is 6, which is ok as I want the exact match, but I want to exclude the test of class and id, so that the result I get is 4 and not 6.

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

0

You can form a regex that will match and capture the substrings you would like to skip when counting and match the asset in other contexts only.

The sample regex may look like

/\b((?:id|class)="Test\b)|\bTest\b/gi

This will match

  • \b((?:id|class)="Test\b) - word boundary, Group 1 capturing id or class, then ="Test as a whole word
  • | - or
  • \bTest\b - whole word Test

See the JavaScript demo:

function countOccurences(str,exceptions,word){
   const pattern = "\\b((?:" + exceptions.map(x => x.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')).join("|") + ')="' + word + "\\b)|\\b"+word+"\\b";
   const regex = new RegExp(pattern,"gi");
   let count = 0, m;
   while (m = regex.exec(str)) {
       if (!m[1]) {
           count++;
       }
   }
   console.log(count)
}


let text = ' test TEST TESTING Test I like testing <h3>TEST</h3> class="test id="test" ';

let asset="Test";
let exception_arr = ["id", "class"]
countOccurences(text,exception_arr,asset);
// => 4

Another solution is based on the negative lookarounds (not support in all JavaScript environments yet):

function countOccurences(str,exceptions,word){
   const pattern = "\\b(?<!\\b(?:" + exceptions.map(x => x.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')).join("|") + ')=")'+word+"\\b";
   const regex = new RegExp(pattern,"gi");
   console.log((str.match(regex) || ['']).length);
}

let text = ' test TEST TESTING Test I like testing <h3>TEST</h3> class="test id="test" ';

let asset="Test";
let exception_arr = ["id", "class"]
countOccurences(text,exception_arr,asset);

Here, /\b(?<!\b(?:id|class)=")Test\b/gi regex will match any Test as a whole word if it is not immediately preceded iwth id or class as whole words followed with =" substring.

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