Javascript regex. Split string into 4 parts.


In javascript I have the following variable:
bounds = "((-37.79235294704733, 144.93249893188477), (-37.78875959854708, 144.93704577956873))"

Open in new window

I need to split each number into its own variable. ie:
a= -37.79235294704733
b= 144.93249893188477
c= -37.78875959854708
d= 144.93704577956873

Open in new window

How can I do this in Javascript? Is this a job for regex?

Who is Participating?
RobConnect With a Mentor Owner (Aidellio)Commented:
I used a bit of regex but it just saves a few lines:

var bounds = "((-37.79235294704733, 144.93249893188477), (-37.78875959854708, 144.93704577956873))";

var newbounds = bounds.replace(/[\(\) ]+/g,"")
var myarray = newbounds.split(',');

Open in new window

If you were to output the myarray variable:

["-37.79235294704733", "144.93249893188477", "-37.78875959854708", "144.93704577956873"]

in other words you can get each value as myarray[0], myarray[1], myarray[2], myarray[3]

a basic test here:
mhdiAuthor Commented:
Perfect. Thankyou
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.