The purpose of this assignment is to familiarize myself with regular expressions and how to use them.
Regex is short for regular expression. A regex is a string of characters for describing a search pattern.
The regex I will be explaining is how to search and match for a hex value. The code snippet for this regex is /^#?([a-f0-9]{6}|[a-f0-9]{3})$/.
- Anchors
- Quantifiers
- Grouping Constructs
- Bracket Expressions
- Character Classes
- The OR Operator
- Flags
- Character Escapes
-
The first element of the regex is the
^symbol. This means the pattern following must appear at the very start of the string. It ensures that the match must occur at the beginning of the input. -
The last element of the regex is
$. This means the pattern preceding must appear at the very end of the string. It ensures that the match must occur at the end of the input.
-
The
?is a quantifier that tells means "zero or one" of the preceding character or group. In this regex, it is saying that the#is optional. Meaning, the hex value may or may not have a#symbol in front of it since they are not always necessary. -
The
{n}quantifier means "exactly n" of the preceding group. In the context of[a-f0-9]{6}, it means exactly 6 characters are either digits0-9or lower case lettersathroughf. In the context of[a-f0-9]{3}, it means exactly 3 characters are either digits0-9or lower case lettersathroughf.
- The
()are used as the grouping constructs in this regex example. It is grouping[a-f0-9]{6}and[a-f0-9]{3}together. That way, the regex will match either exactly 6 characters from the[a-f0-9]or exactly 3 characters from the[a-f0-9]group.
- The
[]are used to designate a bracket expression. The search will match anything in the brackets. In this example, they are enclosing the character classesa-f0-9.
- A character class matches any one character from a set of character defined within the bracket expression
[]. In this example, it isa-f0-9. This means to match any lowercase letterathroughfand any digit0-9.
- The OR operator in this regex is the
|symbol. This means that either expression to the left or right of the symbol can be returned in a search, since they are in the same grouping construct().
My name is Dominic DeCapite and I am a student in a Full Stack Coding Bootcamp. I wrote this tutorial on a hex value regex to better familiarize myself with how regexs operate and what the symbols represent. A link to my Github profile can be found here.