Skip to content
Advertisement

How can i get the red green and blue values from an rgb/rgba string?

If I have the string rgba(111,222,333,0.5) how can i extract the individual colours from the string, i.e.

JavaScript

I would like to be able to use a neat clean solution for this so I am assuming a regular expression would be best?

Advertisement

Answer

I’d avoid regex for a predictable string, and suggest:

JavaScript

JS Fiddle demo.

Or, given that you want an object returned:

JavaScript

JS Fiddle demo.

Edited to use more contemporary JavaScript:

JavaScript

References:

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