0

Trying to mask the US number using reg expression, I am able to format it but not able to mask properly.

This is a number which I am converting to standard format.

const phoneNo = '4345345345';
const formatted = phoneNo.replace(/(\d{3})(\d{3})(\d{4})/, '($1) $2-$3')
console.log(formatted)
output: "(434) 534-5345"

Now I need to mask it in this given format.

output: "(***) ***-5345"

I not able to implement a viable solution for this. Although I am able to implement using vanilla JavaScript.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 1
    So instead of `'($1) $2-$3'` use `'(***) ***-$3'` – Gabriele Petrioli Nov 28 '22 at 06:36
  • you can just do this `phoneNo.replace(/(\d{3})(\d{3})(\d{4})/, '(***) ***-$3')`. no? – Layhout Nov 28 '22 at 06:36
  • Yes I did that @GabrielePetrioli . But I felt implementing this way can cause security hazard. – Hassan Qamar Nov 28 '22 at 06:41
  • well the suggested changes, as well as your original code, will not handle telephone strings that are not exactly 10 digits. We do not have more context to understand about the security hazard you mention. If you could add more info to your question related to security perhaps we can suggest something else. – Gabriele Petrioli Nov 28 '22 at 07:39

0 Answers0