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

103
Visualizações
Concept of a static variable inside a function when called by string.replace

I have the following code to do a basic IP validation. Actually, it's just academic and I'm using this code to practice using String.replace (intentionally using a poor/loose regex):

let potential_ips = [
    '1.2.3.4.5',
    '12.34.45.56',
    '123.456.789.0',
    '1.2.3.4.5.6'
] 
var group_num = 0;
function rePlace(m, g1) {
    group_num ++;
    if (parseInt(g1) > 255) {
        return '<invalid>'
    } else  if (group_num > 4) {
        return '<invalid>'
    } else {
        return m;
    }
}
const regex = /(\d{1,3})\.?/g;
for (ip of potential_ips) {
    group_num = 0;
    ip = ip.replace(regex, rePlace);
    console.log('222', ip);
}

What I'm trying to do is when the .replace function is called to keep a running total of the number of times it's been called for that particular string. Currently I'm using a var at the top-level scope, but I'm wondering if there is a sort of cleaner way to do this within the function itself? What would be a better approach to doing this?

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

0

You could take a closure over the count and initialize with zero.

const
    rePlace = count => m => {
        count++;
        if (parseInt(m) > 255 || count > 4) return '<invalid>';
        return m;
    },
    regex = /\d+(?=(\.|$))/g,
    potential_ips = ['1.2.3.4.5', '12.34.45.56', '123.456.789.0', '1.2.3.4.5.6'];

for (let ip of potential_ips) {
    ip = ip.replace(regex, rePlace(0));
    console.log('222', ip);
}

about 4 years ago · Juan Pablo Isaza Relatório

0

The concept here is copied from Nina's answer, but here is one approach where the rePlace function now returns a bound function with an initialized variable to zero (perhaps a bit like Church numerals?)

function rePlace(count) {
    return function(m) {
        count ++;
        return (parseInt(m) > 255 || count > 4) ? '<invalid>' : m;
    }
}

const regex = /\d+(?=(\.|$))/g
const potential_ips = ['1.2.3.4.5', '12.34.45.56', '123.456.789.0', '1.2.3.4.5.6'] ;
for (let ip of potential_ips) {
    ip = ip.replace(regex, rePlace(0));
    console.log('222', ip);
}

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