A Quick Regular Expression (Regex) to Pull Colors From a CSS Stylesheet

less than 1 minute read | Suggest an edit | Issue? Question?

This one’s mostly for my quick personal reference. It wasn’t hard by any means but figured someone else might find it useful.

Problem

I need to find all hex color codes in a style sheet so I can extract them.

Solution

I came up with a quick Regex (regular expression) to do this:

    #([A-Fa-f0-9]*);

Essentially, it just looks for a pound symbol (#), followed by any amount of numbers 0-9 or letters a-f, followed by a semi-colon.

If you’re interested to try it, you can view my example over at Regex Tester.

It’s not the slickest but it got the job done quickly.

Happy coding!

Updated:

Leave a comment