I am trying to compose a regexp in JS for RGB color string with optional opacity value. To get the data for each channel and opacity I am capturing named groups.
Everything works as intended, but there is something strange for me. When the "B" value in range [200-255], the last digit always goes to opacity group...
For example:
rgb 195 230 199 -> r: 195, g: 230, b: 199 (All is fine)
rgb 195 230 199 0.25 -> r: 195, g: 230, b: 199, opacity: 0.25 (All is fine)
rgb 195 230 200 -> r: 195, g: 230, b: 20, opacity: 0 (Not fine)
And this strange behavior works the same for range 200-255. And spend literally and hour trying to solve it, please, I need some help.
The full regexp is:
^(?:rgb|rgba)?[\s+\/]*\(?[\s+\/]*(?<r>[01]?\d\d?|2[0-4]\d|25[0-5])[\s+\/]+(?<g>[01]?\d\d?|2[0-4]\d|25[0-5])[\s+\/]+(?<b>[01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])(?:\s)?(?<opacity>0\.\d{1,2}|[1-9]\d{0,1}(?!\d)|100|0|1)??(?:\%)?\)?$
Broken into parts:
^
(?:rgb|rgba)?
[\s+\/]*
\(?
[\s+\/]*
(?<r>[01]?\d\d?|2[0-4]\d|25[0-5])
[\s+\/]+
(?<g>[01]?\d\d?|2[0-4]\d|25[0-5])
[\s+\/]+
(?<b>[01]?\d\d?|2[0-4]\d|25[0-5])
(?:\s)?
(?<opacity>0\.\d{1,2}|[1-9]\d{0,1}(?!\d)|100|0|1)??
(?:\%)?
\)?
$
^(?:rgba?)?[\s\/]*\(?[\s\/]*(?<r>[01]?\d\d?|2[0-4]\d|25[0-5])[\s\/]+(?<g>[01]?\d\d?|2[0-4]\d|25[0-5])[\s+\/]+(?<b>[01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\s?\b(?<opacity>0\.\d{1,2}|[1-9]\d?(?!\d)|100|0|1)?%?\)?$regex101.com/r/9dxpVj/1.match()/.split()/.join()and save a handful of CPU cycles.