Skip to content

Instantly share code, notes, and snippets.

@keberlea
Last active May 24, 2023 05:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keberlea/30c5eee1c9bdbf00d763b3f1e32f9ac4 to your computer and use it in GitHub Desktop.
Save keberlea/30c5eee1c9bdbf00d763b3f1e32f9ac4 to your computer and use it in GitHub Desktop.
Regular Expressions

Matching a Hex value using regular expresssion (regex)

Welcome to this informative walkthrough on utilizing the following regular expression (regex) /^#?([a-f0-9]{6}|[a-f0-9]{3})$/ to match and validate hexadecimal color values. This guide will equip you with the knowledge and skills ot search for specfic colors or validate user input for color values. Discover how this regexn efficiently handles both three and six-character hexadecimal color formats, with or without the preceding '#' symbol. Wether you're a developer seeking to extract color codes from codebases or ensure valid color inputs from users, this walkthrough will provide step-by-step instructions and practical examples.

Get ready to harness the potential of this regular expression and enhance your color-related coding tasks.

Summary

In this walkthrough, we'll explore the regular expression /^#?([a-f0-9]{6}|[a-f0-9]{3})$/ and its application in matching and validating hexadecimal color values. This regex allows you to identify and verify color codes in both three and six-character formats, whether they include the '#' symbol or not. Through step-by-step instructions and code examples, you'll learn how to utilize this regex to search for specific colors in codebases or validate user input for color values. By the end of this walkthrough, you'll be equipped with the skills to effectively handle color-related coding tasks using this powerful regular expression.

Table of Contents

Regex Components

Anchors

The anchors in this code ensures that from the begining to the end, the pattern matches.

1. ^ - This denotes the start of a line and ensures that the pattern matches from the beginning of a line.

2. $ - This denotes the end of a line and ensures that the pattern matches until the end of a line.

Examples: The start of the pattern would be following the ^ allowing all matches to start with or without the # and to end after either 3 or 6 digits. The result is a patterns like #000, #000000, 000, and 00000.

Quantifiers

The quantifiers allow for flexibility in the pattern matching by specifying the number of occurrences or making certain parts of the pattern optional.

1. ? - This is applied to the preceding # symbol. It denotes that the preceding character or group (in this case, #) is optional. It allows for zero or one occurrence of #.

2. {6} - This is applied to the character class [a-f0-9]. It specifies that the preceding character or group must occur exactly six times. In this case, it ensures that the pattern matches exactly six hexadecimal characters.

3. {3} - This is also applied to the character class [a-f0-9]. It specifies that the preceding character or group must occur exactly three times. It ensures that the pattern matches exactly three hexadecimal characters.

Example: Only three or six-digit codes can match so #0F9 for green but #00FF99 would also match the digit quantifiers and the ? would allow for 0F9 and 00FF99 to match without the preceding #.

Grouping Constructs

The grouping construct is represented by parentheses ( ). In this case, the entire pattern [a-f0-9]{6}|[a-f0-9]{3} is enclosed within parentheses. This grouping allows the alternation (|) to apply to the entire pattern within the parentheses.

This captures either a six-digit hexadecimal color code or a three-digit hexadecimal color code with the ('|') acting as logical OR, allowing either of the patterns to match.

Example: Either a six-digit or three-digit code can match, so 000 for black would match but so would 000000.

Bracket Expressions

The bracket expressions in this code are represented by [] and are used to define a character class, specifying a set of characters that can match at that point in the pattern.

1. [a-f0-9] - This character class matches any lowercase letter from a to f or any digit from 0 to 9.

This ensures that only valid hexadecimal characters are matched.

Example: Only letter a-f and 0-9 match, and example of this would be aabbcc to match a shade of gray, or FFA500 to match orange.

Character Classes

N/A

The OR Operator

The OR operator in this regex is represented by the '|'. It allows for either of the patterns to it's left or right to match, acting as a logical OR.

The OR operator in this regex seperatres the two following patterns for matching either a six-digit or a three-digit hexadecimal color code.

1. [a-f0-9]{6} - This pattern matches exactly six hexadecimal characters.

2. [a-f0-9]{3} - This pattern matches exactly three hexadecimal characters.

Example: FF0000 and 000 would match for the color red and the color black respectively.

Flags

N/A

Character Escapes

N/A

Author

Alicia Keberle is a University of Oregon x EdX bootcamp student with the desire to learn most efficient ways to finding and validating user inputs utilizing regex equations. Find out more about Alicia Keberler on GitHub.

Title (replace with your title)

Introductory paragraph (replace this with your text)

Summary

Briefly summarize the regex you will be describing and what you will explain. Include a code snippet of the regex. Replace this text with your summary.

Table of Contents

Regex Components

Anchors

Quantifiers

Grouping Constructs

Bracket Expressions

Character Classes

The OR Operator

Flags

Character Escapes

Author

A short section about the author with a link to the author's GitHub profile (replace with your information and a link to your profile)

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