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

162
Visualizações
how to replace all in a given string except the last 4 digits like **** **** **** 4587

"\\d(?=\\d{4})","*"

I've tried it like that but it does not seem to be working it gives me the exact same string i entered to the scanner

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

0

Maybe match on a simple regex to get each group of numbers, then map over that array to produce the desired result, joining it up into a new string at the end.

const str = '1234 1234 1234 5678';
const re = /(\d+)/g;

const out = str
  .match(re)
  .map((el, i, arr) => {
    if (i < arr.length - 1) return '****';
    return el;
  })
  .join(' ');
  
console.log(out);

about 4 years ago · Juan Pablo Isaza Relatório

0

You can simply slice the string and save its four-last characters and append it to the result string after your done with the replacement

let str = '6135 4639 4990 1032'

function hideChars(str) {
    const stars = str.slice(0, str.length - 4).replace(/\d/g, "*")
    const fourLast = str.slice(str.length - 4, str.length)
    return stars + fourLast 
}
const result = hideChars(str)
// result = **** **** **** 1032
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