javascriptregexgmail

Removing dots and plus from gmail address


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:

  1. Delete all dots . that occur before @
  2. Delete all plus + and everything following up to @
  3. 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);


Solution

  • Simpler is to first split the string at the @ character. Then clean up the first part, and put them back together.

    function clean_name(address) {
      let [localpart, domain] = address.split('@');
      if (domain.match(/^(gmail|googlemail)\.com$/i)) {
        localpart = localpart.replace(/\+.*|\./g, '');
      }
      return localpart + '@' + domain;
    }
      
    
    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 results = tests.map(clean_name);
    console.log(results);