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 );
}

For Cut-and-Paste