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

398
Visualizações
My Regexp doesnt work correctly, but work on the regexr website

I've a problem with my regex. I dont know why, it's really strange but, I obtain some great result with console.log and not with my condition.

This is my code :

    const regEx = new RegExp("((?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[!\"#$%&'()*+,-./:;<=>?@[\\]^_\`{|}~]).{"+k+"})", 'g');
    let possibilities = 0;
    for(let i = 0; i < string.length; i++){
        if(string.length<i+k) break;
        const s = string.slice(i, i+k);
        console.log(regEx.test(s), s)
        if(regEx.test(s)) possibilities++;
    }
    return possibilities;

function count(n, k, string) {
    const regEx = new RegExp("((?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[!\"#$%&'()*+,-./:;<=>?@[\\]^_\`{|}~]).{"+k+"})", 'g');
    let possibilities = 0;
    for(let i = 0; i < string.length; i++){
        if(string.length<i+k) break;
        const s = string.slice(i, i+k);
        console.log(regEx.test(s), s)
        if(regEx.test(s)) possibilities++;
    }
    return possibilities;
}
console.log(count(30,6,"G=d:Dl:T=9NS1c$9qC%,^EdUVLnU-7"))

On my function, I get 0 possibilities, whereas I should receive 11. And when I count the true values of the consoles.log, I get 11 ... Do you know why I've this problem, and how I can solved it ?

And, when I test all the string on regexr, it's work...

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

When you're doing console.log(regEx.test(s), s) you're consuming the RegEx once and move an internal cursor or something.

You need to create the new RegExp inside the loop scope, so its cursor will be placed back at the begining and won't be consumed at each iterations.

Plus, you may want to store the result in a variable if you want to use it twice or more :

function count(n, k, string) {
    
    let possibilities = 0;
    for(let i = 0; i < string.length; i++){
        const regEx = new RegExp("((?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[!\"#$%&'()*+,-./:;<=>?@[\\]^_\`{|}~]).{"+k+"})", 'g');
        if(string.length<i+k) break;
        const s = string.slice(i, i+k);
        const regexResult = regEx.test(s);
        console.log(regexResult, s)
        if(regexResult) possibilities++;
    }
    return possibilities;
}
console.log(count(30,6,"G=d:Dl:T=9NS1c$9qC%,^EdUVLnU-7"))

about 4 years ago · Santiago Trujillo 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