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

218
Visualizações
how to create regex for (-) not repeat after 3 digit inside formik setfieldvalue

i have regex function like this:

const format = (value) => {
    if (typeof value === 'string') {
      return value.replace(/(\d{3})(\d+)/g, '$1-$2');
    }
}

when i console without inputing into formik setfieldvalue it produce correct regex like 111-1111111:

const custom = (value) => {
   console.log('valuemain',format(value)); //this valuemain: 111-1111111
   formikRef.current.setFieldValue('customnumb',value)
}

but when i inputing regex into formik setfieldvalue it becomes like 111-111-111:

const custom = (value) => {
   console.log('valuemain',format(value)); //this valuemain: 111-111-111
   formikRef.current.setFieldValue('customnumb',format(value))
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Replace beforehand all occurrences of hyphen "-" with "" (empty string)

const format = (val = "") => val.replace(/-/g, "").replace(/(\d{3})(\d+)/g, '$1-$2');

console.log(format("1111111111"));        // "111-1111111"
console.log(format("111-1111111"));       // "111-1111111"
console.log(format("111-11-1-1-1-11"));   // "111-1111111"

Since obviously you're only interested in integers; to be politically more correct, instead of replacing only the hyphens "-" you could replace (using RegExp \D Not a Digit) — everything that is not a digit:

const format = (val = "") => {
  // @TODO: do val checks if needed here.
  return val
    .replace(/\D/g, "")                 // Remove everything that is not a digit
    .replace(/(\d{3})(\d+)/g, '$1-$2');  // Format as desired
};

console.log(format("1111111111"));        // "111-1111111"
console.log(format("111-1111111"));       // "111-1111111"
console.log(format("111-11-1-1-1-11"));   // "111-1111111"

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