Skip to content

Instantly share code, notes, and snippets.

@jfocha
jfocha / regex-hex-value.md
Last active August 30, 2021 05:54
Using a Regular Expression or 'regex' to Match a Hex Value

Using a Regular Expression or 'regex' to Match a Hex Value

What Is a Regex? And how does this combination of charactersc find any hex value in a document? Well, read on dear Coder because I will be explaining this in this tutorial.

Summary

So, the regular expression /^#?([a-f0-9]{6}|[a-f0-9]{3})$/ defines a search pattern to find hex codes that are used to code colors. Let's break down exactly how the regex does that here in this tutorial. Specifically, let's break down the characters in the regex and explain what they do. Regex contain literal characters and meta characters. Using an escape character or changing the order of a character in the regex will determine if the character is literal or meta. Literal characters are like their name implies. They are the actual character. For example, the character 'a' literally means 'a'. As opposed to a meta character which does something to the search and does not mean the actual character that's typed. For example, the string 'abc*' means find strings tha