How can I partially hide email address like this in Javascript?
[email protected] => ex**pl**ai*@domain.com
I have modify the below code, but can get the result that I need, it's just return this result:
exam*******@domain.com
email.replace(/(.{4})(.*)(?=@)/, function (gp1, gp2, gp3) {
for (let i = 0; i < gp3.length; i++) {
gp2 += "*";
}
return gp2;
});
You could seach for a group of four characters and replace a group of two until you found an @ sign-
const
mask = string => string.replace(
/(..)(.{1,2})(?=.*@)/g,
(_, a, b) => a + '*'.repeat(b.length)
);
console.log(mask('[email protected]'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With