- Regular expressions, or regex, refer to encoded text strings designed to match patterns in other strings.
- These are helpful when you need to find a string of characters that match a type of pattern.
- These patterns can be simple, very complex.
- One of the more common ones is for email address validation.
function validate(input) { let mailformat = /^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:.[a-zA-Z0-9-]+)*$/ if (mailformat.test(String(input).toLowerCase())) { console.log("Hey , Mail is Correct") } else { console.log("This is not a valid email address"); } } validate('s@gmail.com') // Hey , Mail is Correct validate('sgmailcom') // This is not a valid email address
- An email is a string (a subset of ASCII characters) separated into two parts by the
@
symbol. - A "email name" and a domain, that is
mailName@domain.
- The length of the email name part may be up to 64 characters long, and the domain name may be up to 253 characters.