문제

So I am having a string that is created via function and then another function gets that string and takes only the numbers out of it. The string always looks like this (only the numbers change):

"rgba(123,213,321,0.23)"

So I want to get an array from this string like this:

strArray[0] == 123
strArray[1] == 213
strArray[2] == 321
strArray[3] == 0.23

I want to get both round and decimal numbers in one array. I am using this:

strArray = str.match(/(\d+)/g);

But it doesn't do the job because the decimal number is rounded. This means that when I have the same string as above, I get:

strArray[0] == 123
strArray[1] == 213
strArray[2] == 321
strArray[3] == 0

I also have to point out that I am not quite experienced with regex (actually not experienced at all). I got this line of code from somewhere. So I would be thankful if you explain the regex part in your answer (if there is one).

도움이 되었습니까?

해결책

You can use this regex:

strArray = str.match(/(\d+(?:\.\d+)?)/g);
//=> ["123", "213", "321", "0.23"]
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top