DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Javascript regular expression to grab filename from URI  (Read 11,614 times)

Ampa

  • I am cute ;)
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 592
Javascript regular expression to grab filename from URI
« on: April 02, 2008, 05: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)
http://website/subdir/xx title of the image.jpg
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)
title of the image
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
« Last Edit: April 02, 2008, 05:47 PM by Ampa »

Ampa

  • I am cute ;)
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 592
Re: Javascript regular expression to grab filename from URI
« Reply #1 on: April 02, 2008, 06:12 PM »
Here is how I am doing it without using a regex...

Code: (javascript)
var splitArray = this.preLoader.src.replace(/\%20/g, ' ').split('/');
var IMGTitle = splitArray[splitArray.length-1];
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?

mhb

  • There is no life before coffee!
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 22
Re: Javascript regular expression to grab filename from URI
« Reply #2 on: April 02, 2008, 07: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)
^.*/\d{2}\s(.*)\..*$
$1

In JavaScript this could be done like this:

Code: (javascript)
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
There's nothing new under the sun,
but there are lots of old things we don't know. (Ambrose Bierce)
« Last Edit: April 02, 2008, 07:28 PM by mhb »

Ampa

  • I am cute ;)
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 592
Re: Javascript regular expression to grab filename from URI
« Reply #3 on: April 02, 2008, 11:54 PM »
Thanks Marc, I shall try these out  :)

mwb1100

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,645
Re: Javascript regular expression to grab filename from URI
« Reply #4 on: April 03, 2008, 12:21 AM »
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.

Ampa

  • I am cute ;)
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 592
Re: Javascript regular expression to grab filename from URI
« Reply #5 on: April 03, 2008, 12:39 AM »
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 :)

Original address: https://www.donationcoder.com/forum/index.php?topic=12909.0