Skip to content

Instantly share code, notes, and snippets.

@geraldyeo
Created June 29, 2016 03:31
Show Gist options
  • Save geraldyeo/866bfdc071d9962f2206a1b9538e8407 to your computer and use it in GitHub Desktop.
Save geraldyeo/866bfdc071d9962f2206a1b9538e8407 to your computer and use it in GitHub Desktop.
Regex for HEX/RGB(A)/HSL(A)
// Use simple one, if you need to find a definitely correct HEX/RGB(A)/HSL(A)
// color value among random text (limitations are set by its initial correctness):
/(#[\d\w]+|\w+\((?:\d+%?(?:,\s)*){3}(?:\d*\.?\d+)?\))/i
// …or use complex one instead, if you need to find (or not) one (or several)
// HEX/RGB(A)/HSL(A) color value(s) that has to be verified:
/(#(?:[\da-f]{3}){1,2}|rgb\((?:\d{1,3},\s*){2}\d{1,3}\)|rgba\((?:\d{1,3},\s*){3}\d*\.?\d+\)|hsl\(\d{1,3}(?:,\s*\d{1,3}%){2}\)|hsla\(\d{1,3}(?:,\s*\d{1,3}%){2},\s*\d*\.?\d+\))/gi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment