Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created March 24, 2014 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bennadel/9751624 to your computer and use it in GitHub Desktop.
Save bennadel/9751624 to your computer and use it in GitHub Desktop.
Ask Ben: Extracting Parts Of An Email Address In Javascript
function GetEmailParts( strEmail ){
// Set up a default structure with null values
// incase our email matching fails.
var objParts = {
user: null,
domain: null,
ext: null
};
// Get the parts of the email address by leveraging
// the String::replace method. Notice that we are
// matching on the whole string using ^...$ notation.
strEmail.replace(
new RegExp( "^(.+)@(.+)\\.(\\w+)$" , "i" ),
// Send the match to the sub-function.
function( $0, $1, $2, $3 ){
objParts.user = $1;
objParts.domain = $2;
objParts.ext = $3;
}
);
// Return the "potentially" updated parts structure.
return( objParts );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment