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

150
Visualizações
.match() matches only some of an identical array of strings

I'm matching an array of strings against a regex:

for (let subject_index in subjects) {
      if (subjects[subject_index].match(/.*Overview of Investor.*/i)) {
        subjects.splice(subject_index, 1)
      }
    }

The array subjects contains nine identical strings, all of them Overview of Investor. When I run this loop, it correctly matches five of the strings and does not match on the other four, even though all the strings are identical. Am I missing something?

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

0

You're editing the array while iterating over it, which can cause weird problems. You can do this in a better way with Array.filter.

subjects = subjects.filter(subject => !subject.match(/.*Overview of Investor.*/i));

This way, you are not editing the array while reading the elements.

Edit: As noted in the comments, this solution removes the matched strings, because that is what the original poster's solution is trying to do. If you want to keep the matched strings, use

subjects = subjects.filter(subject => subject.match(/.*Overview of Investor.*/i));

This is the same code but without the exclamation point.

about 4 years ago · Santiago Gelvez Relatório

0

Do not mutate your array within your loop. Create a new array to add your matches to.

let subjects = ["Overview of Investor", "Overview of Investor", "Overview of Investor", "Overview of Investor", "Overview of Investor", "Overview of Investor", "Overview of Investor", "Overview of Investor", "Overview of Investor", "Overview of Investor", "Overview of Investor"];
let ans = [];

for (let subject_index in subjects) {
      if (subjects[subject_index].match(/.*Overview of Investor.*/i)) {
        ans.push(subjects[subject_index]);
      }
 }

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