The following regular expression validates a user's input to check that it has all the components of a valid email address.
^([a-z0-9_.-]+)@([\da-z.-]+).([a-z.]{2,6})$
This regex checks to see if the input before the "@" contains numbers or letters, has a period or hyphen to separate values, then checks for an "@" symbol,then checks for letters for the domain name after the "@", checks that there's a dot after the domain, and lastly checks for another set of letters with a minimum value of 2 and a maximum of 6 letters after the ".".
All instances of quotes, or " in this gist are to denote an exact aharacter match, such as "@" saying that the literal "at" symbol is being refernced.
- "^" asserts position at start of the string,
- "^" matches the start of a string without consuming any characters,
- "$" matches the end of a string without consuming any characters,
- "@" matches the literal character "@",
- "." checks for the period character,
- "{2, 6}" checks for a string between 2-6 letters
- The "^" quantifier tells you if there are exact character matches and groups of character matches of a specific size.
- The "+" quantifier asserts a reition of similiar sharacters to the one immedietely before the "+".
- Another example of a quantifier in this regex is the {2,6} which checks that there a minimum of 2 and a maximum of 6 characters after the period following the domain name.
- In this regex the parentheses surrounding characters denote a grouping construct.
- The first group asserts that a series of characters may exist with a period or underscore in between them.
- The second group checks for a series of alphabetic characters following the "@" symbol.
- The final group uses a quantifier to check for between 2-6 alphabetical acharacters that collow the period, for example ".com" and ".org"
- In the only bracket expression in this regex is "{2,6}" and it defines that the length of the string after the period can be between 2 and 6 characters
- In the square brackets in this regex define a specific group or type of characters to look for that are present, which in this regex are aplhanumeric characters, a period or underscore, and the "@" symbol.
- A backslash before a character defines a specific type of regex value rather than the character following it.
- Example: "\d" Represents a digit rather than just the letter "d".
Matthew Brignola PrismaticDevs
Character classes and bracket expressions are basically the same thing.