I am trying to create a regular expression that validates an red/green/blue value expression. The expressions that I need my regular expression to match include:
- rgb(1, 10, 100)
- rgb(100, 10, 1)
- rgb(1,2,3)
- rgba(1, 2, 3, .75)
- rgba(1, 2, 3, 0.75)
- rgba(1, 2, 3, 0)
- rgba(1, 2, 3, 1)
Currently, I have the following regular expression:
^rgba?\((([+-]?\d+(\.\d+)?),\s*){2}[+-]?\d+(\.\d+)?(,\s*(0?\.\d|[01]))?\)$
This expression works for #1, #2, #3, #6, and #7. However, it fails for #4 and #5. I keep staring at the last group. To me, it looks like it should accept a decimal. Clearly, it's not though. What am I doing wrong?