Is there a good strategy for cleaning up user inputted Gmail addresses of the form
my.ac.ct@gmail.com
myacct+001@gmail.com
To be the actual address? ie
myacct@gmail.com
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 = `foo.bar@gmail.com
foobar+secret@gmail.com
foo.b.......a.r..@gmail.com
.foob.ar.+..++..123..@googlemail.com`;
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);
You need to sign in to view this answers
Leave feedback about this