-1

I have an email in the following format:

joe+12312313@aDomain.com

First, I need to make sure the email's domain equals to aDomain.com Next, I need to extract everything before the + sign

It would be nice if I can get a the following object:

var objParts = {
        localPart: null,
        domain: null,
        };

console.log(objParts.localPart) // joe
console.log(objParts.domain) // aDomain.com

I know we have to use Regex. I am new to JS.

user1107173
  • 10,334
  • 16
  • 72
  • 117

3 Answers3

2

It doesn't seem like you have to validate the generic email format here.

You can just split on the main points, @ and +, and extract the data:

const email = 'joe+12312313@aDomain.com'
const domain = email.split('@').pop()  // split on '@' and get the last item
const local = email.split('+').shift() // split on '+' and get the first item

console.log(domain);
console.log(local);
nem035
  • 34,790
  • 6
  • 87
  • 99
2

var email = "joe+12312313@aDomain.com";
var objParts = CreateEmailParts(email);

console.log(objParts.localPart);
console.log(objParts.domain);


function CreateEmailParts(email)
{
  if(email)
  {
        var objParts = {
          domain:  email.split('@')[1], // caution: hoping to have the domain follow @ always
          localPart: email.split('+')[0], // caution: hoping to have the ema follow @ always
        };
 return objParts;
  }
}

https://jsfiddle.net/pdkvx82d/

Jaya
  • 3,721
  • 4
  • 32
  • 48
1

Use simple split:

var str = "joe+12312313@aDomain.com";
var parts = str.split("@");
var objParts = {
    localPart: parts[0].split('+')[0],
    domain: parts[1],
};
console.log(objParts);
u_mulder
  • 54,101
  • 5
  • 48
  • 64