Fix the Regex Pattern Matcher
MediumChallenge 19 of 20
Fix the Regex Pattern Matcher
MediumThis function is supposed to validate email addresses using a regular expression, but it's not working correctly for all valid email formats. Fix the implementation.
Challenge 19 of 20
50 XP
Community Solutions
Regular ExpressionsValidationStringsJavaScript
Exit ChallengeTest Cases
0/4 passingTest Case #1
Input: isValidEmail('user@example.com')
Expected: true
Run tests to see results
Test Case #2
Input: isValidEmail('user.name@example.co.uk')
Expected: true
Run tests to see results
Test Case #3
Input: isValidEmail('user+tag@example.com')
Expected: true
Run tests to see results
Test Case #4
Input: isValidEmail('invalid@')
Expected: false
Run tests to see results
solution.js
1
<span class="token keyword">function</span> isValidEmail(email) {
<span <span class="token keyword">class</span>=<span <span class="token keyword">class</span>="token string">"token comment"</span>>// This regex is supposed to validate email addresses</span>
<span class="token keyword">const</span> emailRegex = /^[a-zA-Z0-<span <span class="token keyword">class</span>="token number">9</span>]+@[a-zA-Z0-<span <span class="token keyword">class</span>="token number">9</span>]+\.[a-zA-Z]{<span <span class="token keyword">class</span>="token number">2</span>,}$/;
<span class="token keyword">return</span> emailRegex.test(email);
}Console
Run your code to see output here