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

118
Views
How Can I dynamically mask all digits except the last 4 always?

How Can I mask all the digits that the user to input, dynamically?

Scenario 1: User input: 1234 5678 9123 4414 Output: xxxx xxxx xxxx 4414

Scenario 2: User input: 12345678 8234245 Output: xxxxxxxx xxx4245

Scenario 3: User input: 12 345678911 Output: xx xxxxx8911

What I have is a fix only and it is static, How can I make my code to be dynamic? so that I can lessen my if else statement?

function hideMask(num) {
    
  var regExp = /[a-zA-Z]/g;
  
  if(regExp.test(num)){
     return null;
  } else {
    if(num.replace(/\s/g, '').length == 16){ // 16 digit
    mask = num.substring(num.length - 14).replace(/\d/g,"x");
    unmaskCardNumber = num.substring(14, 19);
    return(mask + unmaskCardNumber);
  }else if(num.replace(/\s/g, '').length == 18){ //18 digit
    mask = num.substring(0,15).replace(/\d/g,"x");
    unmaskCardNumber = num.substring(15, 19);
    return(mask + unmaskCardNumber);
  }else{
    return null;
   }
  }
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can easily achieve the result using split, reverse and map

function mask(s) {
  let count = 0;
  return s
    .split("")
    .reverse()
    .map((n, i) => (!n.match(/\d/) ? n : count < 4 ? (count++, n) : "x"))
    .reverse()
    .join("");
}

function mask(s) {
  let count = 0;
  return s
    .split("")
    .reverse()
    .map((n, i) => {
      if (!n.match(/\d/)) return n;
      else {
        return count < 4 ? (count++, n) : "x";
      }
    })
    .reverse()
    .join("");
}

console.log(mask("1234 5678 9123 4414"));
console.log(mask("12345678 8234245"));
console.log(mask("12 345678911"));
console.log(mask("12 345678 9 1 1")); //  CORNER CASE

You can even skip reverse step if you use reduceRight as:

function mask(s) {
  let count = 0;
  return s
    .split("")
    .reduceRight((acc, n, i) => {
      acc.push(!n.match(/\d/) ? n : count < 4 ? (count++, n) : "x");
      return acc;
    }, [])
    .reverse()
    .join("");
}

function mask(s) {
  let count = 0;
  return s
    .split("")
    .reduceRight((acc, n, i) => {
      acc.push(!n.match(/\d/) ? n : count < 4 ? (count++, n) : "x");
      return acc;
    }, [])
    .reverse()
    .join("");
}

console.log(mask("1234 5678 9123 4414"));
console.log(mask("12345678 8234245"));
console.log(mask("12 345678 9 1 1"));

about 4 years ago · Juan Pablo Isaza Report

0

I would go about it, using slice, and replace: First replace all alphanumeric characters in the string, bar the last four with 'x', and append the last four from before:

function mask(input) {
  return input
    .slice(0, input.length - 4)
    .replace(/([a-zA-Z0-9])/g, 'x') + input.slice(-4)
}
console.log(mask('abcd efgh 1234 5678'))

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!