@CarlHumm
Posted
Hi there
Before regular expressions can be used, they have to be compiled.
The literal notation results in compilation of the regular expression when the expression is evaluated. Sources: MDN
To evaluate an expression you can use one of the prototype methods available to you - the most common being .prototype.test() which returns a true/false Boolean.
Using the strict equality operator like in the example below will not compile and evaluate the regex.
if (email.value === pattern) {
email.style.border = "2px solid green";
}
The following however will compile and evaluate the regex against the specified email input. Within the conditional we are saying if this test fails, then it's not valid.
if(!pattern.test(email.value)) {
console.log('That email is not valid!');
}
Hope this helps :)
Marked as helpful