0

I need to split an email address and take out the first character and the first character after the '@'

I can do this as follows:

'bar@foo'.split('@').map(function(a){ return a.charAt(0); }).join('')
--> bf

Now I was wondering if it can be done using a regex match, something like this

'bar@foo'.match(/^(\w).*?@(\w)/).join('')
--> bar@fbf

Not really what I want, but I'm sure I miss something here! Any suggestions ?

6
  • I wonder if this would be faster. Why do you want to use regex ? Commented Jun 25, 2014 at 8:45
  • @Oliboy50: see for yourself. Regex is faster than the inital code posted by the OP, but it's still nowhere near as fast as my suggestion Commented Jun 25, 2014 at 9:07
  • @EliasVanOotegem My suggestion will be faster: stackoverflow.com/a/24404355/1249581. Commented Jun 25, 2014 at 9:09
  • @VisioN: I saw your answer, added it to the jsperf: it's faster than the regex match version, but no where near as fast as my suggestion Commented Jun 25, 2014 at 9:13
  • 1
    @VisioN: well obviously that would be a problem, but then the alternatives listed here would fail silently in the sense that they'd return something else than 2 chars Commented Jun 25, 2014 at 9:27

6 Answers 6

3

Why use a regex for this? just use indexOf to get the char at any given position:

var addr = 'foo@bar';
console.log(addr[0], addr[addr.indexOf('@')+1])

To ensure your code works on all browsers, you might want to use charAt instead of []:

console.log(addr.charAt(0), addr.charAt(addr.indexOf('@')+1));

Either way, It'll work just fine, and This is undeniably the fastest approach
If you are going to persist, and choose a regex, then you should realize that the match method returns an array containing 3 strings, in your case:

/^(\w).*?@(\w)/
["the whole match",//start of string + first char + .*?@ + first string after @
 "groupw 1 \w",//first char
 "group 2 \w"//first char after @
]

So addr.match(/^(\w).*?@(\w)/).slice(1).join('') is probably what you want.

Sign up to request clarification or add additional context in comments.

Comments

3

If I understand correctly, you are quite close. Just don't join everything returned by match because the first element is the entire matched string.

'bar@foo'.match(/^(\w).*?@(\w)/).splice(1).join('')
--> bf

Comments

1

Using regex:

matched="",
'abc@xyz'.replace(/(?:^|@)(\w)/g, function($0, $1) { matched += $1; return $0; });

console.log(matched);
// ax

Comments

1

The regex match function returns an array of all matches, where the first one is the 'full text' of the match, followed by every sub-group. In your case, it returns this:

bar@f
b
f

To get rid of the first item (the full match), use slice:

'bar@foo'.match(/^(\w).*?@(\w)/).slice(1).join('\r')

Comments

1

Use String.prototype.replace with regular expression:

'bar@foo'.replace(/^(\w).*@(\w).*$/, '$1$2');  // "bf"

Comments

0

Or using RegEx

^([a-zA-Z0-9])[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@([a-zA-Z0-9-])[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$

Fiddle

4 Comments

This pattern doesn't come close to being useful to the OP. It's an insufficient email validation pattern, not a pattern to get specific chars out of the string
@Elias Van Ootegem The pattern above does exactly that is needed. Yes, it validates an email, but it also extracts the first chars from the two parts of email address as well.
But why would the OP use this overly complex pattern? The pattern he has returns (close to) the same match. The only difference is that he needs to s[p]lice the result array before calling join
@Elias Van Ootegem Well, the OP asked for a regex :) But yes, the pattern itself may be less complex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.