Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expression and filename building 1

Status
Not open for further replies.

Kazim

Programmer
Jul 23, 2001
26
GB
I have a series of files that are named in this way:

Filename_FileNumber_Description.htm

I need to load these files into an iFrame but the only information I have to work on is the filename and the filenumber. Of course the .htm is always at the end of the name.

This leaves me with:

iframe.src = Filename_Filenumber_ ..... .htm

I need to be able to use some kind of regular expression (perhaps similar to *.*) to match the filenames by their number, no matter what the description might be.

BTW: The description is all alphanumeric chars.

Could anyone help me please.
 
Unless you have the actual full file name, you cannot set the src of an iframe - since you cannot put a regular expression into an html tag.

Do you have an array of the actual file names? If so, then it would be possible to iterate through the array until you find a file that matches the pattern. Without the full file names to begin with, it's not possible.

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]

What is Javascript? faq216-6094
 
Aha! Yes I do have a list of the filenames (Or at least I can make one because they are my files). But how would I work through the list. It will grow on a regular basis as more files are added.

I'm afraid JS and arrays are not my thing at all!
 
Well... you might do it like this:
Code:
// create your data array here
var dataArr = ['filename_1_blue.html',
'filename_33_yellow.html',
'filename_12_red.html',
'filename_76_orange.html'];

// populate the 2 known variables
var _filename = 'filename';
var _number = '76';

var matchedfile = '';
var re = eval("/"+_filename+"_"+_number+"_[^.]*\.html/i");
for (var loop=0, num=dataArr.length; loop<num; loop++) {
  if (dataArr[loop].match(re)) {
    matchedfile = dataArr[loop];
    break;
  }
}

// alert the results
if (matchedfile == '') alert('no match');
else alert(matchedfile);

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]

What is Javascript? faq216-6094
 
Excellent! Thats exactly what I needed! It works 100% with just a tiny tweek!

Thanks very much indeed!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top