How to identify a given string is hex color format

前端 未结 7 2375
走了就别回头了
走了就别回头了 2020-12-07 15:27

I\'m looking for a regular expression to validate hex colors in ASP.NET C# and
am also looking code for validation on server side.

For instance: #CCCCCC

相关标签:
7条回答
  • 2020-12-07 15:55

    Based on MSalters' answer, but preventing an incorrect match, the following works

    ^#(([0-9a-fA-F]{2}){3}|([0-9a-fA-F]){3})$
    

    Or for an optional hash # symbol:

    ^#?(([0-9a-fA-F]{2}){3}|([0-9a-fA-F]){3})$
    

    And without back references being generated:

    ^#?(?:(?:[0-9a-fA-F]{2}){3}|(?:[0-9a-fA-F]){3})$
    
    0 讨论(0)
  • 2020-12-07 15:55

    Ruby

    In Ruby, you have access to the \h (hexadecimal) character class. You also have to take more care of line endings, hence the \A...\z instead of the more common ^...$

    /\A#(\h{3}){1,2}\z/
    

    This will match 3 or 6 hexadecimal characters following a #. So no RGBA. It's also case-insensitive, despite not having the i flag.

    0 讨论(0)
  • 2020-12-07 15:57

    Minor disagreement with the other solution. I'd say

    ^#(([0-9a-fA-F]{2}){3}|([0-9a-fA-F]){3})$
    

    The reason is that this (correctly) captures the individual RGB components. The other expression broke #112233 in three parts, '#' 112 233. The syntax is actually '#' (RR GG BB) | (R G B)

    The slight disadvantage is more backtracking is required. When parsing #CCC you don't know that the second C is the green component until you hit the end of the string; when parsing #CCCCCC you don't know that the second C is still part of the red component until you see the 4th C.

    0 讨论(0)
  • 2020-12-07 15:59

    all answers mentioned RGB format, here is regex for ARGB format:

    ^#[0-9a-fA-F]{8}$|#[0-9a-fA-F]{6}$|#[0-9a-fA-F]{4}$|#[0-9a-fA-F]{3}$
    
    0 讨论(0)
  • 2020-12-07 16:03
    ^#(?:[0-9a-fA-F]{3}){1,2}$
    

    Dissection:

    ^              anchor for start of string
    #              the literal #
    (              start of group
     ?:            indicate a non-capturing group that doesn't generate backreferences
     [0-9a-fA-F]   hexadecimal digit
     {3}           three times
    )              end of group
    {1,2}          repeat either once or twice
    $              anchor for end of string
    

    This will match an arbitrary hexadecimal color value that can be used in CSS, such as #91bf4a or #f13.

    Note: No support for RGBA hex color values, though.

    0 讨论(0)
  • 2020-12-07 16:04

    This if you want to accept named colors and rgb(a,b,c) too. The final "i" is for case insensitive.

    HTML colors (#123, rgb not accepted)

    /^(#[a-f0-9]{6}|black|green|silver|gray|olive|white|yellow|maroon|navy|red|blue|purple|teal|fuchsia|aqua)$/i
    

    CSS colors (#123, rgb accepted)

    /^(#[a-f0-9]{6}|#[a-f0-9]{3}|rgb *\( *[0-9]{1,3}%? *, *[0-9]{1,3}%? *, *[0-9]{1,3}%? *\)|rgba *\( *[0-9]{1,3}%? *, *[0-9]{1,3}%? *, *[0-9]{1,3}%? *, *[0-9]{1,3}%? *\)|black|green|silver|gray|olive|white|yellow|maroon|navy|red|blue|purple|teal|fuchsia|aqua)$/i
    
    0 讨论(0)
提交回复
热议问题