Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Use regular expression within an array

    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?

  • #2
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    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:

    Code:
    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
      }
    }
    Trinithis

  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Trinithis View Post
    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:

    Code:
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •