Check if a value is a valid hexadecimal color code

By Hunter Becton on January 21, 2023

const isHexColor = (hex) => {
  const regex = /^#?([0-9A-Fa-f]{6}|[0-9A-Fa-f]{3})$/;
  return regex.test(hex);
}

How to use

This function returns true or false depending if the value passed in follows the hexadecimal color code syntax.