Regex for matching CSS hex colors

后端 未结 5 1078
傲寒
傲寒 2021-02-02 07:33

I\'m trying to write regex that extracts all hex colors from CSS code.

This is what I have now:

Code:

$css = <<

        
5条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-02 08:09

    Despite this question's age I'd like to ammend the following:

    ^#([[:xdigit:]]{3}){1,2}$, where [[:xdigit:]] is a shorthand for [a-fA-F0-9].

    So:
    [[:xdigit:]]{3}){1,2}$/", $css, $matches) ?>

    Also noteworthy here is the usage of a non-capturing group (?>...), to ensure we don't store data in memory we never wanted to store in the first place.

    Try it online

提交回复
热议问题