Regex (Javascript) for parsing email with [email protected]

375 Views Asked by At

I've scoured all the other threads about this but there's a unique aspect to my parsing format that I can't get past. I'm trying to parse an email address with a standard format as follows - firstname.lastname.#####@email.com i.e '[email protected]'

I want to return any alpha characters before the numeric sequence - 'john smith' (or all names before the .12345 in case people have multiple names).

Right now, I've worked out the regex to (^[A-Za-z]+\.+[A-Za-z]+) but that returns 'john.smith' which isn't the end of the world since I can split it.

Any help would be much appreciated.

1

There are 1 best solutions below

0
zr0gravity7 On

Click on the code blocks below to try them out.

If you only have two or three dot-separated names before the numbers, you can hardcode the group alternatives:

(?:([A-Za-z]+)\.([A-Za-z]+)|([A-Za-z]+)\.([A-Za-z]+)\.([A-Za-z]+))\.\d+@[^\s@]+

Here we use a non-capturing group to enclose the alternatives.

For one, two, or three dot-separated names, the pattern is similar (albeit a bit messier).


Another approach would be to wrap some of the preceding dot + capturing group sections with an optional non-capturing group.

([A-Za-z]+)\.([A-Za-z]+)(?:\.([A-Za-z]+))?\.\d+@[^\s@]+

This enables a more concise expression of 1-3 dot-separated names as:

([A-Za-z]+)(?:\.([A-Za-z]+))?(?:\.([A-Za-z]+))?\.\d+@[^\s@]+