A regex, which is short for regular expression, is a sequence of characters that defines a specific search pattern. When included in code or search algorithms, regular expressions can be used to find certain patterns of characters within a string, or to find and replace a character or sequence of characters within a string. They are also frequently used to validate input.
I will be teaching you how to make a regex for an email validation and describing which parts of the regex describe what.
/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/
- Anchors
- Quantifiers
- Grouping and Capturing
- Bracket Expressions
- Greedy and Lazy Match
- Character Escapes
The ^ anchor signifies a string that begins with the characters that follow it. And the $ anchor signifies a string that ends with the characters that precede it. In our example we can see our expression enclosed with those anchors /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/
Quantifiers set the limits of the string that your regex, or an individual section of it, matches. it can be a single character like * or it could be multiple characters inside curly brackets like {3} indicating a size of 3 or {3,16} indicating a size between 3 and 16 just like in our example.
In order to make a more complex regex you will need to group sections of it with different requirements. The way to group is to use parenthesis (). Everything in the parenthesis will have the same requirements for that section of the expression. In our example, /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/ you can see that we have 3 different groups seperated by some requirements ([a-z0-9_\.-]+) & ([\da-z\.-]+) need to be seperated by @ and ([\da-z\.-]+) & ([a-z\.]{2,6}) need to be seperated by .
Bracket expressions or defined using brackets [] and represents a range of characters that we want to match. They are used to include all of the characters we want to match. for example [a-z] matches all lowercase letters and [0-9] matches all numbers. They can also include special characters like in our example [a-z0-9_\.-] represents all lowercase letters, numbers, and underscores, periods and dashes.
Greedy and Lazy matches refer to how we choose the string being affected by each group. A greedy match will try the longest possible string and a lazy match will try the shortest possible string. Our expression uses greedy methods on each group which is indicate by the + at the end of the groups or by the quantifier {3,16} which is inherently greedy.
Character escapes are represented with a backslash \ and are used for characters that are used to define the regex itself but are wanted as a requirement inside the regex. For example, the open curly brace { is used to begin a quantifier, but adding a backslash before the open curly brace \{ means that the regex should look for the open curly brace character instead of beginning to define a quantifier.
- Github: ThatBallinGuy
- Email: [email protected]