Skip to content

Instantly share code, notes, and snippets.

@eikouji
Last active March 18, 2022 03:06
Show Gist options
  • Save eikouji/5e7670ff87be4344036da93d7a1a30e1 to your computer and use it in GitHub Desktop.
Save eikouji/5e7670ff87be4344036da93d7a1a30e1 to your computer and use it in GitHub Desktop.
Regex Tutorial Gist -- Matching a Hex Value

A Regex Tutorial, Matching a Hex Value

This is my example of a Regex Tutorial about how matching a Hex value works

Summary

This is a tutorial about a Regex. I will explain this line of code: Matching a Hex Value - /^#?([a-f0-9]{6}|[a-f0-9]{3})$/

Table of Contents

Regex Components

We have the Regex, let us study: /^#?([a-f0-9]){6}|[a-f0-9]{3})$/

Anchors

For this regex, the ^ is the start of the expression and the $ is the end of the expression.

Quantifiers

the symbol # in this case refers to how hex codes show up within code.

There is usually a # before a hex code, such as #e65c00.

Screen Shot 2022-03-01 at 10 06 53 AM

The ? is saying there may or may not be a # to indicate there is a hex code after the #. If there is, check for it. If not, still look for the rest of the regex values.

OR Operator

in this regex example, the Order operator is represented by ([a-f0-9]{6}|[a-f0-9]{3}), so, ([]{6}|[]{3}]). a(b|c) means search a, then for b followed by c. In my hex code regex, it says look for 6 characters first, and then, search for 3 characters.

Character Classes

[a-f] - any alphabetic letters between a through f. Lower case a, b, c, d, e, and or f. [0-9] - any digit or numeric number between 0 and 9. 0, 1, 2, 3, 4, 5, 6, 7, 8, and or 9. In any arrangemenet.

Flags

no flags in this expression

Grouping and Capturing

{6}|{3}

This is saying look for any text that contains 6 characters of letters between a - f, and or numbers between 0 - 9. Any arrangement of letters between those letters, and any arrangement of numbers.

OR look for 3 characters, either of letters between a - f, and or numbers between 0 - 9. Includes any arrangement of letters and numbers.

Bracket Expressions

the section ([a-f0-9){6}|[a-f0-9{3}]);

the a-f is looking for any characters in the alphabet between a through f, so; a, b, c, d, e, and or f. The letters included in hex codes.

the 0-9 is looking for any digits between 0 and 9, so; 0, 1, 2, 3, 4, 5, 6, 7, 8, and or 9.

the {6} looks for 6 characters with either letters between a through f, or numbers between 0 and 9. For example, #ff0000 is a red. #ffffff is the color white. #0066ff is a rich sky blue color. Screen Shot 2022-03-01 at 10 05 54 AM

Boundaries

The boundaries in this regex are the 's, the ones before the ^ and after the $, so. ^(regex)$.

Author

Tutorial by: Eiko Ujifusa

I found a lot of great info on this website here: https://regex101.com/r/iRxFEX/1 I used the w3 schools color picker for each beautiful hex code color example:

Regex 101 screen cap of regex with sample text:

regex101 screencap of sample text

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment