I want to mask email address [email protected]=> [email protected] in JavaScript
i used /(?<=.{2}).(?=[^@]*?@)/ regex but not working in internet explorer and Mozilla so i need regex which works in all browser(JavaScript)
maskedEmail = stringObj.replace(/(?<=.{2}).(?=[^@]*?@)/g, "X");
I want to mask email address [email protected]=> [email protected] in JavaScript
i used /(?<=.{2}).(?=[^@]*?@)/ regex but not working in internet explorer and Mozilla so i need regex which works in all browser(JavaScript)
maskedEmail = stringObj.replace(/(?<=.{2}).(?=[^@]*?@)/g, "X");
Share Improve this question edited Jul 6, 2019 at 10:05 Parth Patel 91913 silver badges35 bronze badges asked Jul 6, 2019 at 10:00 KhonKhon 2755 silver badges15 bronze badges2 Answers
Reset to default 6Try this option, which avoids using a lookbehind:
var email = "[email protected]";
var output = email.replace(/^(.{2})[^@]+/, "$1XXX");
console.log(email);
console.log(output);
Note that I took a slight shortcut here by always representing the third character of the email name onwards with static XXX
. We could try to keep the same length name, but it would take more work. I actually advise against doing that, because it partially defeats the purpose of masking the email, by giving away the actual length.
There is no need of regex for this simple operation. This should work equally well:
var emailAddress = "[email protected]"
function maskEmail(email) {
let split = email.split('@')
return email.substr(0,1) + new Array(split[0].length - 1).fill('x').join('') + "@" + split[1]
}
console.log(maskEmail(emailAddress))