Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialToheeb Ashorobi
Full Stack JavaScript Techdegree Student 9,685 Pointswhy isn't there any letters in the Email validator?
I don't understand why there isn't any letters within this section of the validation email function. Aren't you supposed to have [a-z] or even \w somewhere to allow the user to input the beginning part of the email?
[^@]+@[^@.]+
return /[^@]+@[^@.]+\.[a-z]+/.test(email);
1 Answer
Dane Parchment
Treehouse Moderator 11,077 PointsIt does! So let's break down the regex for you
[^@]
- So this creates a grouping that matches any single character that isn't an @
symbol. So it'll match any letter/number/symbol. For example this would match: a
, 9
, or &
.
+
- Is a greedy quantifier. What this means is it takes the preceding item and matches it an unlimited number of. times. For example /a+/
will match all of the a's in the word: aaaaaaaple
. In our case what this does is make the previous items which was [^@]
match unlimited amount of times, i.e: hello_world
would be a valid matching.
@
- A specific character match, that looks for this specific symbol. Due to the grouping earlier what that means is that this will basically repeat the greedy matching until it reaches this @
symbol and includes it in the match. So basically this is our [a-z]
but is more specific. This would match: hello_world123@
.
[^@.]
- This next part creates another grouping that once more will match anything that isn't an @
symbol or a period. This is basically allowing us to enter anything after the @
symbol that isn't a period or another @
. This would match: hello_world123@e
.
+
- Like before is a greedy quantifier that will make this check the previous statement an unlimited amount of times. In our case it just matches anything after the @
that isn't also an @
or period. This would match: hello_world123@email
\.
- This is basically just a specific character match for a period. Since periods are used for other things we need to escape it first, hence the backslash. But basically this stops the greedy quantifier at the first period it encounters and includes it in the match. So this would match: hello_world123@email.
[a-z]
- Next we have this grouping that checks for any letter between a and z inclusive. So it will match any letter in the alphabet that is lowercase, as this is case sensitive. This would match: hello_world123@email.c
+
- Finally we end things with another greedy quantifier that basically checks the previous item an unlimited amount of times. In our case it will just keep matching lowercase letters of the alphabet. So it basically checks for the final part of the email, that com
, edu
, gov
, companydomain
part. It would match: hello_world123@email.com
Hope that helps you understand!