Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

161
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda