This is a tutorial about Regex, or regular expression. What is Regex? Regex is a string of text that helps us create patterns in order to search for text. The following document will walk you through how to use Regex to match an email address.
We will use this Regex below for this example
/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/
/: Begin Regex
^: Match any string it start with
( and ) : Create a group
[ and ]: Begin and End bracket list
[a-z0-9_\.-]+: This will match any letters and numbers. The a-z means any letters between the two. The 0-9 means any numbers between 0 and 9. It also accepts characters _. -, and \. means escaping sequence denoting the character .. + means any of those characters can be repeated more than one
@: This is the @ required for an email format
[\da-z\.-]+: This will take any letters. Also \d means it will match a single character that is a digit. \. means escaping sequence denoting the character ..+ means any of those characters can be repeated more than one
\.: the . before any extension
[a-z\.]{2,6}: This means it can take any letters from a-z with the total minimum length of 2 and maximum length of 6
$: anchor at the end of the string
- Anchors
- Quantifiers
- OR Operator
- Character Classes
- Flags
- Grouping and Capturing
- Bracket Expressions
- Greedy and Lazy Match
- Boundaries
- Look-ahead and Look-behind
The anchors stay at the begining and the end of the string. As we can see from the example above, we have ^ and $
^: match any string that start with. For example^an, any string starts withansuch asanythingwill be a match$: match any string that end with. For exampleer$, any string that ends withersuch asteacherwill be a match
Quantifiers define specific requirements of an input for a match to appear. In the example above, we have + and {2,6} are representations of what quantifiers are
+: means one or more. For example,xyz+means that a string has to havexyand follow by one ore morez.{2,6}: provides a minimum and maximum characters. This is considered a fixed quantifier.
This is used to match THIS or THAT
|is used as an OR operator to indicate that it can be matched with one or more patterns. For examplethis|that: a string that has eitherthisorthat
This is used to include any character match requirement
[a-z]: match all letters between a-z Some other common ones that are not used in the example above\d: match a single character that is a digit.: match all characters\w: match a word character
At the end of a regex, we can put a flag to dictate some value. For example,
i: means insensitive, which ignores case-sensitive lettersg: means global, which will not return the first match but also start subsequent search at the end of the 1st match- and so on...
This help organize characters to extract information needed
(,): used to group characters
Expression that use brackets
[,]: used to define character class. For example[a-z]means any string that has at least one letter range between a to z
A greedy match try to get as many matches as possible while a lazy match will try to only get one match Some common examples are :
*+and{}
This helps with whole words search if you need to find an exact match to a certain string
\b: for example\bhi\b, this will search for the wordhiexactly how it is spelled out within the boundary
Look ahead:
?=: for examplea(b?=)means it will only match withaif theais followed by ab, but thebis not a part of the regex match Look behind:?<=: for examplea(b?<=)means it will only match withaif theahas abright before, but thebis not a part of the regex match
Reference material: https://medium.com/factory-mind/regex-tutorial-a-simple-cheatsheet-by-examples-649dc1c3f285 My name is Anh Vuong. I am a student of UT coding bootcamp seeking a career in web development. Here is my github: https://github.com/avuong19