Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

148
Views
.match() coincide solo con algunas de una matriz idéntica de cadenas

Estoy haciendo coincidir una matriz de cadenas con una expresión regular:

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

La matriz subjects contiene nueve cadenas idénticas, todas ellas Overview of Investor . Cuando ejecuto este bucle, coincide correctamente con cinco de las cadenas y no coincide con las otras cuatro, aunque todas las cadenas son idénticas. ¿Me estoy perdiendo de algo?

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

Está editando la matriz mientras itera sobre ella, lo que puede causar problemas extraños. Puedes hacer esto de una mejor manera con Array.filter.

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

De esta manera, no está editando la matriz mientras lee los elementos.

Editar: como se indica en los comentarios, esta solución elimina las cadenas coincidentes, porque eso es lo que intenta hacer la solución del cartel original. Si desea mantener las cadenas coincidentes, utilice

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

Este es el mismo código pero sin el signo de exclamación.

about 4 years ago · Santiago Gelvez Report

0

No mute su matriz dentro de su bucle. Cree una nueva matriz para agregar sus coincidencias.

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!