Vishera Vishera - 10 days ago 5
Javascript Question

extract first and last name from pattern email address

I am trying to get the First Name and Last Name out of a series of email addresses. There is a pattern for each e-mail address. The pattern is first.last@company.com but in some cases, it can be something like first.middle.last@company.com. The main point being that there can be something in between the first and last name in the e-mail but not always.
The main pattern, which is always the same, is that the first string, until the first dot is always the first name, and the last name is always the last character series until the @ char which begins from the previous DOT.
Examples:

charles.bukowski@company.com
first: charles
last:bukowski

charles.mark.bukowski@copmany.com
first: charles
last:bukowski

charles.x.markus.bukowski@company.com
first: charles
last:bukowski

Getting the first name is easy. I have the following variables:

var empmail = 'charles.x.markus.bukowski@company.com'
var empname = empmail.substring(0, empmail.indexOf("."));


I cannot find the way to get out the last name.

Answer

Use String.prototype.split() method:

var empmail = 'charles.x.markus.bukowski@company.com'

var fullName = empmail.split('@')[0].split('.');

var firstName = fullName[0];
var lastName = fullName[ fullName.length-1 ]