I’m having real difficulty with this but I’m no javascript expert. All I want to do is get myself an array of all matches in a string which match a given regExp. The regExp being this :
[0-9]+
ie. Any integer.
So If I pass the string “12 09:8:76:::54 12” I should get
arr[0]=”12″
arr[1]=”09″
arr[2]=”8″
arr[3]=”76″
arr[4]=”54″
arr[5]=”12″
Easy? Not for me! I could do this in vb.net no problem with regexp.matches(string) (something like that anyway). I thought that the javascript method .exec would also give me an array however it only returns the first match. What’s going on? Code…
function testIt(){
splitOutSelection2("123:::45 0::12312 12:17");
}
function splitOutSelection2(sSelection){
var regExp = new RegExp("[0-9]+","g");
var arr = regExp.exec(sSelection);
};
All the answers work but I was wanting to keep my regExp object rather than specify it at the time of use. So simply changing the function to…
..is what I was looking for. Thanks for pointing me in the right direction though to all who have replied.