Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

163
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!