0

I have some string like below :

"linear-gradient(to bottom,rgba(255,0,0,0.4) 0,rgba(0,230,175,0.4) 100%);"

i want to extract all rgba and rgb from this stirng like rgba(255,0,0,0.4) and rgba(0,230,175,0.4) . it try to search and find some question like this and this but that answer doesn't help me.

2 Answers 2

3

I want to extract all rgba and rgb from the stirng

I hope you don't want to validate the digits in rgba and rgb, if yes then get the matched group from index 1 using capturing groups.

(rgba?\((?:\d{1,3}[,\)]){3}(?:\d+\.\d+\))?)

Live demo


You can get the values using Lazy way as well in the same way from matched group index 1.

(rgba?.*?\))

Live Demo

Last Pattern explanation:

  (                        group and capture to \1:
    rgb                      'rgb'
    a?                       'a' (optional)
    .*?                      any character except \n (0 or more times)
    \)                       ')'
  )                        end of \1
Sign up to request clarification or add additional context in comments.

Comments

1

If you just want to match the strings, for instance rgba(255,0,0,0.4), without parsing the values, just use:

arrayOfMatches = yourString.match(/rgba\([^)]+\)/g);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.