Is there a good strategy for cleaning up user inputted Gmail addresses of the form
[email protected]
[email protected]
To be the actual address? ie
[email protected]
The use case is to disallow creating multiple website accounts that have distinct gmail addresses yet they all point to the same gmail inbox. The "normalized" email would be stored in a separate field in the database, so that when any new user signs up we can easily check the normalized new user email address vs the normalized existing emails.
Here’s what I came up with and a code example:
- Delete all dots
.
that occur before@
- Delete all plus
+
and everything following up to@
- Delete the
oogle
out of @googlemail.com
These 3 match operations or’ed together in this regex
/.+(?=.*@(gmail|googlemail).com)|+.*(?=@(gmail|googlemail).com)|(?<=@g)oogle(?=mail.com)/gi
It works on the test cases below, it’s not very polished. Is there another technology that is more effective?
const teststr = `[email protected]
[email protected]
[email protected]
[email protected]`;
const tests = teststr.split("n");
const re = /.+(?=.*@(gmail|googlemail).com)|+.*(?=@(gmail|googlemail).com)|(?<=@g)oogle(?=mail.com)/gi;
const results = tests.map(t => t.replace(re, ""));
console.log(results);
2
Answers
Simpler is to first split the string at the
@
character. Then clean up the first part, and put them back together.I made the solution into the function
parseEmails(emails)
:Note that I’m using your Regular Expression. There’s a much better one for testing valid gmail adresses that I’m also using as an optional final validator: