Skip to content
Advertisement

Javascript – Match and parse Hsl color string with Regex

I’m trying to parse a hsl color string and get an hex color string from it. I tried using regex but can’t figure it out. How my regexp should be look like to match and parse a hsl color string to hue, saturation and value fileds. The input will be one of the belows;

 - hsl(162,11.984633448805383%,81.17647058823529%)
 - hsl(162, 11.984633448805383%, 81.17647058823529%) <= there are
   space's between fields.

Thanks.

Advertisement

Answer

This is probably how I’d handle it

/hsl((d+),s*([d.]+)%,s*([d.]+)%)/g

Regular expression visualization

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement