Regex for hex color code matching in JavaScript

Hex colors come in 3- and 6-digit forms with a leading #. This pattern matches both. Test your CSS values against it live below.

Working example
/#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})/g

Try it live

The pattern and sample strings below are pre-loaded. Edit anything, or describe a variation in plain English and generate your own.

/#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})/gvalid ✓

Tested live on the JavaScript engine. Other flavors are validated and flagged for differences, not executed.

No samples.

Related regex recipes