Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partially mask email address - javascript

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;

});

like image 879
AmJaradat01 Avatar asked Oct 29 '25 09:10

AmJaradat01


1 Answers

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]'));
like image 155
Nina Scholz Avatar answered Oct 31 '25 23:10

Nina Scholz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!