Skip to content

Instantly share code, notes, and snippets.

@jessangarcia
Last active January 22, 2023 01:46
Show Gist options
  • Save jessangarcia/1361adf24381b790cdfa03d0002dec77 to your computer and use it in GitHub Desktop.
Save jessangarcia/1361adf24381b790cdfa03d0002dec77 to your computer and use it in GitHub Desktop.
regular expressions? or cat walking on keyboard?

Regular Expressions

The purpose of this tutorial is to show you how to use regular expression. Learning this can help locate snipets of code with almost all programming languages. You can use these to find certain combinations within a string. I will be using this gist to identify some of these expressions.

Summary

/^#?([a-f0-9]{6}|[a-f0-9]{3})$/

This regular expression is used to match Hex Values. All of this will be broken down, and I will be explaining how these are used in this particular Regex. You can use this site to help further your understanding of Regex https://regexr.com/

Table of Contents

Regex Components

Anchors

^ - Used at the beginning $ - Used at the end

We use anchors to see if the matching symbol is at the start or end. The don't really match a character they just help identify the position before or after a character.

Quantifiers

? - Matches 0 or 1 of the preceding token {6} - Match 6 of the preceding token {3} - Match 6 of the preceding token

Qualifiers in this example tell you how many letters or numbers to match to the token in front of it. The ? is saying to match with # atleast 0 or 1 time, making this an optional requirement. The number in {} is lettin gus know that we need to match a-z0-9 by 6 or 3 characters.

OR Operator

| - Acts like a boolean OR. Matches the expression before or after the |

This just means OR. In this regex it would mean find a match with 6 letter and numbers 'OR' find a match with 3 letters and numbers. This can operate with a group or whole expression.

Character Classes

# - Matches a "#" character [a-f0-9] - Match any character in this set

Character Classes will look up letters a through f, and numbers 0 through 9. This will also include those letters used to create the range. The # used here is telling the expression to find that symbol before the group we call.

Author

GitHub: https://github.com/jessangarcia

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