473,626 Members | 3,936 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regex match of string in array index?

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.jp g",
"photo02.jp g",
"photo02.jp g");

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?imag e02.jpg

I then extract the search string like this:

display = window.location .search;
photo = display.substri ng (display.lastIn dexOf('?') +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?

Oct 31 '05 #1
3 3083
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.jp g",
"photo02.jp g",
"photo02.jp g");

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?imag e02.jpg
Do you mean: ...popup.html?p hoto02.jpg

I then extract the search string like this:

display = window.location .search;
photo = display.substri ng (display.lastIn dexOf('?') +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[i] == 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.jp g" : "caption 1 text",
"photo02.jp g" : "caption 2 text",
"photo03.jp g" : "caption 3 text"
};
...

Having extracted the photo name from the search string:

var caption = photoCaption[photo];

--
Rob
Oct 31 '05 #2
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[i] != photo && images[++i] ) {
current_value = i;
}
if ( !images[i] ) {
// didn't find a match, deal with it
}

[...]
--
Rob
Oct 31 '05 #3
> Do you mean: ...popup.html?p hoto02.jpg

Yes thanks for figuring that and for the various solutions. I'll test.

Nov 4 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3060
by: Tap | last post by:
I would like to parse the following string with as little code as possible. stringValue = "Email Message ID:TAPASVI to tapsbin@hotmail.com;tapasbin@cs.com;{CC}tapstemp@hotmail.com;{CC}tapstemp007@hotmail.com" 1) Take everything in between "ID:" and the word " to" i.e. stringVal1 = "TAPASVI" 2) Take between the word " to " and "{CC}" i.e. stringVal2 = "tapsbin@hotmail.com;tapasbin@cs.com;"
2
317
by: Steve | last post by:
Hi, I have an array of strings e.g. "one","two","three","four","fourteen" From the following string "onethreefourteenten" I would like to know that strings have been matched; strings ,, but not . What i'm trying to get is the longest word beginning at a particular
4
2656
by: Friday | last post by:
Being an Old L.A.M.P guy, I beg you to please excuse my ignorance of dot.net (and all things Windows, for that matter). As part of an experiment (to learn enough ASP/VB.net to port a series of existing PHP scripts of mine to work on IIS), I have created the following simple function to compare a Website visitor's IP address against a varabe-array. The experiment invovleas a common scenario -- banning a Website visitor by IP Address: ...
17
3960
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher http://forta.com/books/0672325667/
5
10177
by: JackRazz | last post by:
Anyone know the regular expression to match a blank line where the byte sequence is "0D 0A 0D 0A" ive tried "\r\n\r\n+", "^$+" "\n\r" with no success. Any Ideas? Thanks - JackRazz This is the code fragment I'm trying Dim r As Regex, m As Match, i As Integer
8
1977
by: Just Me | last post by:
I want to use regular expressions to search a string, give the user the option of replacing, and then maybe replacing the data - using reg expressions for the search and the replace strings. However all the Regex replace methods seem to combine in one call the search and replace. Is there a way of doing what I want?
7
2252
by: lgbjr | last post by:
Hi All, I'm trying to split a string on every character. The string happens to be a representation of a hex number. So, my regex expression is (). Seems simple, but for some reason, I'm not getting the results I expect. Dim SA as string() Dim S as string S="FBE"
5
2420
by: Kofi | last post by:
Any takers? Got a string of DNA as an input sequence GGATGGATG, apply the simple regex "GGATG" as in Regex r = new Regex("GGATG", (RegexOptions.Compiled)); MatchCollection matches = r.Matches("GGATGGATG"); Now I would expect to get two matches right? One at index 0 in the
8
1272
by: Trev | last post by:
Hi, If I had a string denoted by MyArray, how would I extract the number in brackets for use in the programme later on? I've tried "\" but all I get is a syntax error. What would be the usage for using the number in the programme (so that I can check if the index is within bounds)? Talking of groupings, if I had a string like MyRef.MyArray.MyVal how would I extract the strings into an array for processing, so that Array = MyRef, Array =
1
12172
by: jonnyboy6969 | last post by:
Hi All Really hoping someone can help me out here with my deficient regex skills :) I have a function which takes a string of HTML and replaces a term (word or phrase) with a link. The pupose is that I seek out terms which are in a glossary on our site, and automatically link to this definition. Its slightly complex becase certain elements have to be ignored, for exampleI dont want to add links within existing links, or for example link...
0
8266
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8365
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7196
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5574
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.