How to Unmask the first two Numbers in an mobile number

Doable and elegant as a regex, but simpler with manual string manipulation.

If you need some help debugging when you’ve made progress, share your new code and we’ll help you out.

1 Like

Yes, I think you are correct in this case. Sometimes we can try to get too “tricky” when a very simple answer is staring us straight in the face.

1 Like

length of the number is always the same

You seem to be hoping that we will do it for you. We don’t do that here.

So, based on pieces of advice you’ve received so far, what have you tried now?

You always know the length of the number, so you can always determine how many *s you need.


  const last4Digits = mobileNumber.replace(/d{10}(d{4})/, "************$1")
  console.log(last4Digits)

It doesn’t look like you are doing anything to try to get the first digits. Do you still want them?

You need to use \ to get the ‘digit’ character class. ‘d’ is just the letter ‘d’.

Also, currently you are saying that you are checking for 4 digits that follow a 10 digit group. I don’t think that is what you mean. Isn’t 10 digits the length?

Do you have experience with regex?

Do you have experience with string methods? They might be easier if you don’t know any regex.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.