DonationCoder.com Forum

Other Software => Developer's Corner => Topic started by: Ampa on April 02, 2008, 12:43 PM

Title: Javascript regular expression to grab filename from URI
Post by: Ampa on April 02, 2008, 12:43 PM
I am very new to regular expressions and am struggling with something that I am sure is really pretty straight forward.

I have a string from which I need to return the just the filename, without the extension, or the path, or the serial number (xx = two digits at the start of the filename)


Code: Text [Select]
  1. http://website/subdir/xx title of the image.jpg
  2. http://aboutfaceuk.com/gallery/01%20a%20very%20big%20dog.jpg


The top example illustrates the general form of the input text, and the bottom is a real example from the site. The results should be...

Code: Text [Select]
  1. title of the image
  2. a very big dog

Other factors...

In theory there may or may not be a subdir.
The %20 should be replaced with spaces (not entirely sure why they are %20s in the first place, but they are!)
The regex flavour is Javascript.

Help appreciated!

Thanks Ampa
Title: Re: Javascript regular expression to grab filename from URI
Post by: Ampa on April 02, 2008, 01:12 PM
Here is how I am doing it without using a regex...

Code: Javascript [Select]
  1. var splitArray = this.preLoader.src.replace(/\%20/g, ' ').split('/');
  2. var IMGTitle = splitArray[splitArray.length-1];
  3. IMGTitle = IMGTitle.substring(3, IMGTitle.length-4);

A bit long winded perhaps?
1) Replace the %20s with spaces.
2) Split the string at the /s and select the last one.
3) Grab the middle of the string.

Would regex be better?
Title: Re: Javascript regular expression to grab filename from URI
Post by: mhb on April 02, 2008, 02:18 PM
Hi Ampa,

I am not sure if this really helps, since it can't replace the %20.
Nevertheless it replaces the given string with the filename.

Code: Javascript [Select]
  1. ^.*/\d{2}\s(.*)\..*$
  2. $1

In JavaScript this could be done like this:

Code: Javascript [Select]
  1. result = subject.replace(/^.*\/\d{2}\s(.*)\..*$/g, "$1");

I think the best solution for the %20 is to replace these using the JavaScript replace command or using a second regular expression.

Hope it helps...

Marc
Title: Re: Javascript regular expression to grab filename from URI
Post by: Ampa on April 02, 2008, 06:54 PM
Thanks Marc, I shall try these out  :)
Title: Re: Javascript regular expression to grab filename from URI
Post by: mwb1100 on April 02, 2008, 07:21 PM
The %20 should be replaced with spaces (not entirely sure why they are %20s in the first place, but they are!)

Strictly speaking, a URL that's passed to a web server should not have spaces in it (but most browsers and web servers deal with them fine anyway).  You can use javascript's decodeURI() function to convert the %20's (or other encoded characters that might be in the string) to their unencoded form.
Title: Re: Javascript regular expression to grab filename from URI
Post by: Ampa on April 02, 2008, 07:39 PM
mwb1100: thanks for the tip about decodeURI().

I realise that the %20s are usually there for a good reason, but the filename that I extract is not being passed to the browser, it needs to be read by the user, so in this instance spaces are friendlier :)