Skip to content

Instantly share code, notes, and snippets.

@rbenvenuto
Last active July 4, 2018 23:19
Show Gist options
  • Save rbenvenuto/99d738021d88b36be6be06de7046d8c6 to your computer and use it in GitHub Desktop.
Save rbenvenuto/99d738021d88b36be6be06de7046d8c6 to your computer and use it in GitHub Desktop.
JS function that replaces in-between characters with stars
function hiddenEmailAddress(email) {
var emailLength = email.length;
var atPosition = email.search('@');
var unhiddenPortion = email.slice(atPosition, emailLength);
var i;
var hiddenEmail = '';
for(i = 0; i < atPosition; i++) {
if(i === 0 || i === atPosition - 1) {
hiddenEmail += email[i];
} else {
hiddenEmail += email[i].replace(email[i], '*');
}
}
hiddenEmail += unhiddenPortion;
return hiddenEmail;
}
hiddenEmailAddress('[email protected]');
// a*******[email protected]'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment