1

I'm using this code to convert a string like "rgb(200, 12, 53, 0.96)"to an array of numbers

this is my code

var rgb = 'rgb(200, 12, 53, 0.96)'.match(/\d+/g);
console.log(rgb);  

but the output wrongly push 0 and 96 as separate values

how can I keep floating numbers together? i have no idea how regex works sorry

1
  • maybe with simple var rgb = 'rgb(200, 12, 53, 0.96)'.match(/[\d\.]+/g); Commented Oct 9, 2017 at 0:13

1 Answer 1

4

0.96 contains a decimal that doesn't match \d, you can add . with \d in a character class wrapped in [] to match float numbers:

var rgb = 'rgb(200, 12, 53, 0.96)'.match(/[.\d]+/g);
console.log(rgb);  

Sign up to request clarification or add additional context in comments.

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.