...

View Full Version : Use regular expression within an array



Jeff-h
06-17-2008, 09:56 PM
Is this possible?
I am looking to do something like this:

var barcode_number = new Array();
barcode_number["11038801"]="PartNumber1"
barcode_number[Regular Expression here]="PartNumber2"

If so how would I do It?

Trinithis
06-17-2008, 11:20 PM
Barcode_number["11038801"]="PartNumber1"

is possible, but selecting the right index via a regular expression would have to be done through a loop . . . a for-in loop at that:



var assocArr = [];
assocArr["hello"] = "a";
assocArr["hellooo"] = "b";
assocArr["ello"] = "c";

var regex = /^hello+$/;
for(var x in assocArr) {
if(regex.test(x)) {
alert(assocArr[x]);
//break; // uncomment if you want it to stop after 1 match
}
}

oesxyl
06-17-2008, 11:34 PM
Barcode_number["11038801"]="PartNumber1"

is possible, but selecting the right index via a regular expression would have to be done through a loop . . . a for-in loop at that:



var assocArr = [];
assocArr["hello"] = "a";
assocArr["hellooo"] = "b";
assocArr["ello"] = "c";

var regex = /^hello+$/;
for(var x in assocArr) {
if(regex.test(x)) {
alert(assocArr[x]);
//break; // uncomment if you want it to stop after 1 match
}
}

can be done with assocArr.foreach, assocArr.some or maybe assocArr.map ? And if can is any (dis)avantage?

regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum