Home > database >  replacing the phone number with asterisks
replacing the phone number with asterisks

Time:10-06

How do I show only the 4 digits of the number and cover all the others with asterisks? Numbers may have different lengths

I must be doing something wrong, I need to show only the first 4 digits:

var test = '447537126710' // 12 number 15 number phone
//I want to do this: 4475********* 

My script replaces only the first digits, but I need to replace different lengths, and the last digits

CodePudding user response:

You can use a lookbehind assertion to only match digits that are preceded by (at least) 4 other digits:

const test = '447537126710';
console.log( test.replace(/(?<=\d{4})\d/g, '*') )

CodePudding user response:

You can use substring and then replace rest of the characters

var test = '447537126710';
const num = test.substr(0, 4)  
  test.substr(4, test.length).replace(/[0-9]/g, '*')
console.log(num)

  • Related