The purppose of this tutorial is to give users information on what Regex is and how to use it. The tutorial will provide a summary as well as detail examples for a specific regex provide.
Regular Expression, otherwise known as "Regex"; is a sequence of characters that defines a specific search pattern. Regex can be used to find certain patterns of characters within a string. However, Regex is most frequently used to validate inputs from a user. An example is the following Regex provided below.
/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/
The regex is used to verify that the user input is a valid email address. More details of this Regular Expression will be provided below
Anchors allow the strings to match your input. Those anchors would be ^ and $
. The ^
anchor matches the beginning of the string; while the $
anchor matches the end of the string. So for the example of the email validation, "/^
([a-z0-9_.-]+)@([\da-z.-]+).([a-z.]{2,6})$/
", the anchor will match the exact string of the email. The reason is because both characters are at the beginning and end of the validation.
Quantifiers are the characters: *
, +
, ?
, and {}
. Quantifiers match the specified quantity of the previous token. So an example with the email validation, "/^([a-z0-9_.-]+
)@([\da-z.-]+
).([a-z.]{2,6}
)$/", the quantifier will match the tokens from 2 to 6 within {}
and match the string with +
.
OR Operator are the characters: |
and []
. OR Operator matches the characters within the tokens. So an example with the email validation is, "/^([
a-z0-9_.-]
+)@([
\da-z.-]
+).([
a-z.]
{2,6})$/", The []
looks for those tokens and matches them specifically.
Grouping and Capturing are the characters: ()
. It groups multiple tokens together to create a capture group for extracting a substring. So with the email validation, "/^(
[a-z0-9_.-]+)
@(
[\da-z.-]+)
.(
[a-z.]{2,6})
$/", it is capturing the group together within the parentheses.
Bracket Expression uses the characters: []
. Bracket Expressions matches the string between the characters. With the email validation, "/^([a-z0-9_\.-]
+)@([\da-z\.-]
+).([a-z\.]
{2,6})$/", the Bracket Expression matches the characters within []
expresssion.
Greedy operators are the characters: * + {}
. The lazy operator is the character: ?
. Greedy operators match the token to the preceding token. The lazy operator makes the quantifier lazy which makes it match as few characters as possible. So in the email validation, "/^([a-z0-9_.-]+
)@([\da-z.-]+
).([a-z.]{2,6}
)$/", there is no lazy operator. However, it does contain greedy operators which allows the tokens to match specifically.
The gist was created by Juan Garcia. Juan is currently attending a Coding Boot Camp at Michigan State University. He received his Bachelor's Degree in Computer Engineering with a Minor in Computer Science. You can check the links below to know more about Juan: