Regex match of string in array index?

Discussion in 'Javascript' started by Timmy, Oct 31, 2005.

  1. Timmy

    Timmy Guest

    I'm working on a simple click-through image gallery and I have images with
    captions in two arrays like this:

    var current_value="0";

    var images = new Array
    ("photo01.jpg",
    "photo02.jpg",
    "photo02.jpg");

    var captions = new Array
    ("caption1",
    "caption2",
    "caption3");

    The large image views are shown in a window which I can link to with a
    search substring URL defining which image to display: popup.html?image02.jpg

    I then extract the search string like this:

    display = window.location.search;
    photo = display.substring (display.lastIndexOf('?') +1);

    By knowing the search substring value (in this case photo02.jpg) I would
    like a regex function to search through the images array and find the match
    (in this case index 1) and therafter use that index value to set the
    current_value variable.

    Does anyone have an idea how the searching and matching is done?
     
    Timmy, Oct 31, 2005
    #1
    1. Advertising

  2. Timmy

    RobG Guest

    Timmy wrote:
    > I'm working on a simple click-through image gallery and I have images with
    > captions in two arrays like this:
    >
    > var current_value="0";
    >
    > var images = new Array
    > ("photo01.jpg",
    > "photo02.jpg",
    > "photo02.jpg");
    >
    > var captions = new Array
    > ("caption1",
    > "caption2",
    > "caption3");
    >
    > The large image views are shown in a window which I can link to with a
    > search substring URL defining which image to display: popup.html?image02.jpg


    Do you mean: ...popup.html?photo02.jpg

    >
    > I then extract the search string like this:
    >
    > display = window.location.search;
    > photo = display.substring (display.lastIndexOf('?') +1);


    Your posted line will give:

    photo = image02.jpg

    >
    > By knowing the search substring value (in this case photo02.jpg) I would
    > like a regex function to search through the images array and find the match
    > (in this case index 1) and therafter use that index value to set the
    > current_value variable.
    >
    > Does anyone have an idea how the searching and matching is done?
    >


    There are a couple of options, one is to loop through the array looking
    for a matching image name:

    var current_value = 0;
    for (var i=0, n=images.length; i<n && 0 == current_value; ++i){
    if (images == photo) current_value = i;
    }

    If you are only using 'current_value' to get the caption, why not store
    the image name and caption in an object:

    var photoCaption = {
    "photo01.jpg" : "caption 1 text",
    "photo02.jpg" : "caption 2 text",
    "photo03.jpg" : "caption 3 text"
    };
    ...

    Having extracted the photo name from the search string:

    var caption = photoCaption[photo];





    --
    Rob
     
    RobG, Oct 31, 2005
    #2
    1. Advertising

  3. Timmy

    RobG Guest

    RobG wrote:
    [...]
    >
    > var current_value = 0;
    > for (var i=0, n=images.length; i<n && 0 == current_value; ++i){


    Actually that isn't great because if you get a match at i=0 it will
    still loop through all the images. Here's a slightly better version:

    var i=0;
    while ( images != photo && images[++i] ) {
    current_value = i;
    }
    if ( !images ) {
    // didn't find a match, deal with it
    }

    [...]


    --
    Rob
     
    RobG, Oct 31, 2005
    #3
  4. Timmy

    Timmy Guest

    > Do you mean: ...popup.html?photo02.jpg

    Yes thanks for figuring that and for the various solutions. I'll test.
     
    Timmy, Nov 4, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hiwa
    Replies:
    0
    Views:
    640
  2. Jon Perez
    Replies:
    1
    Views:
    1,079
    Peter Otten
    May 28, 2004
  3. Old Echo
    Replies:
    1
    Views:
    182
    Adam Shelly
    Sep 4, 2008
  4. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    306
    Tomasz Chmielewski
    Mar 4, 2008
  5. Tuxedo
    Replies:
    14
    Views:
    485
    Rainer Weikusat
    Nov 30, 2011
Loading...

Share This Page