Q:

Javascript-showing only Last Value from array after end of while loop

var re=/\"travelsName\"\:\"(.*?)\"/g;  //searching global matches and extracting obtained matches in brackets

var data="travelsName":"ddd Travels","travelsName":"nnn Travels","travelsName":"xxx Travels",;//This is a string not json object

var myArray;
                while ((myArray = re.exec(data)) != null)
                {
                     msg = "Found " + myArray[1] + ".  ";

                }

Now i want the whole matched array of myArray[1], which is exact matched version of the regex. 
when i hit alert inside while loop i can see the array values . If i call alert outside the while loop then i am ending up with last array value in alert box which is not i want.
I want whole array outside while loop.
I want array[0]=ddd Travels
array[1]=nnn Travels
etc...
This is done i ajax call and data will be loaded dynamically
Thanks in advance

A:

Try this for your example: 
var re=/\"travelsName\"\:\"(.*?)\"/g;
var data='"travelsName":"ddd Travels","travelsName":"nnn Travels","travelsName":"xxx Travels",';

var myTmpArray;
var myArray = [];
while ((myTmpArray= re.exec(data)) != null) {
    myArray.push("Found " + myTmpArray[1] + ".  ");
}

I'm sure that you can find better solution if you give more infirmation

