Skip to content

Instantly share code, notes, and snippets.

@Sebastian2908-2007
Last active March 29, 2022 19:55
Show Gist options
  • Save Sebastian2908-2007/489582d2a3cea75e23b55a0efb190c31 to your computer and use it in GitHub Desktop.
Save Sebastian2908-2007/489582d2a3cea75e23b55a0efb190c31 to your computer and use it in GitHub Desktop.
A gist about A regex Matching a Hex Value /^#?([a-f0-9]{6}|[a-f0-9]{3})$/

Regex (Matching a Hex Value – /^#?([a-f0-9]{6}|[a-f0-9]{3})$/)

Today it is your lucky day!!! I will bless you with the knowledge of the "Matching A Hex Value " Regular Expression! So have a seat get comfy and read on, be amazed at this knowledge bomb.

Summary

This regex /^#?([a-f0-9]{6}|[a-f0-9]{3})$/) will match a hex value in this tutorial we are going to break it down!

Table of Contents

Regex Components

Anchors

There are two anchors in this expression ^ which means "beginning " and $ which means "end"

Quantifiers

There are actually three "quantifiers in this regular expression! The first being ? which " Matches between 0 and 1 of the preceeding token"! Another quantifier the second in line is {6} this quantifier matches 6 of the preceeding token's the final quantifier in this regex is {3} which as you guessed probably will "match three of the preceeding token's!!!

Alteration

this little symbol | found in this regex"act's like a boolean OR. it Matches the expression before or after the |.!!! Sounds fantastic Right!?

Grouping and Capturing

So there is some capturing going on here! () these surounding "[a-f0-9]{6}|[a-f0-9]{3}"capturing two groups [a-f0-9] of character sets!!

Character Sets

There are two character sets in this regex both being identical to one another [a-f0-9]. Lets break this down so a-f Matches a character in the range of a to f which would be (Charcode 97 to 102). this is also Case Sensitive! The character set 0-9 "Matches a character in the range of 0 to 9 !!! which would be (Charcode 48 to 57)! Which is also case sensitive.

Characters to note

the # symbol in this case matches a "#" it has a (Charcode of 35) How neat!!!

Author

Sebastian Bowen A cool and unique guy you either like him or not...https://github.com/Sebastian2908-2007

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