i have a string which has the following format (3,1,Mayer,Jack). I need all 4 elements as own variables. I thought about using JavaScript’s match()-function to do this, but i don’t know how i can get the 4 elements in variables…
I tried it with this code:
var clickedId = '(3,1,Mayer,Jack)';
var pregId = clickedId.match(/([\d]+,[\d]+,[\w]+,[\w]+)/g);
alert(pregId[0]);
But i get a popup windows with “(3,1,Mayer,Jack)”… I thought about something like this:
var id = pregId[0];
var teamid = pregId[1];
var surname = pregId[2];
var name = pregId[3];
How can i do this?
Thanks!
The
matchfunction is matching the whole expression, not pulling out the parts of the whole. So you need to match on each individual element, say with a regex like(\d|\w)+. Here’s an example:Demo: http://jsfiddle.net/Znu7N/