Created
March 24, 2014 23:29
-
-
Save bennadel/9751624 to your computer and use it in GitHub Desktop.
Ask Ben: Extracting Parts Of An Email Address In Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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