Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • December 08, 2016, 04:10:26 PM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Javascript regular expression to grab filename from URI  (Read 5992 times)

Ampa

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 592
  • I am cute ;)
    • View Profile
    • MonkeyDash - 2 Player strategy boardgame
    • Donate to Member
Javascript regular expression to grab filename from URI
« on: April 02, 2008, 12:43:34 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
« Last Edit: April 02, 2008, 12:47:17 PM by Ampa »

Ampa

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 592
  • I am cute ;)
    • View Profile
    • MonkeyDash - 2 Player strategy boardgame
    • Donate to Member
Re: Javascript regular expression to grab filename from URI
« Reply #1 on: April 02, 2008, 01:12:00 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?

mhb

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 22
  • There is no life before coffee!
    • View Profile
    • Donate to Member
Re: Javascript regular expression to grab filename from URI
« Reply #2 on: April 02, 2008, 02:18:01 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
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, 02:28:46 PM by mhb »

Ampa

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 592
  • I am cute ;)
    • View Profile
    • MonkeyDash - 2 Player strategy boardgame
    • Donate to Member
Re: Javascript regular expression to grab filename from URI
« Reply #3 on: April 02, 2008, 06:54:47 PM »
Thanks Marc, I shall try these out  :)

mwb1100

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,522
    • View Profile
    • Donate to Member
Re: Javascript regular expression to grab filename from URI
« Reply #4 on: April 02, 2008, 07:21:26 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.

Ampa

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 592
  • I am cute ;)
    • View Profile
    • MonkeyDash - 2 Player strategy boardgame
    • Donate to Member
Re: Javascript regular expression to grab filename from URI
« Reply #5 on: April 02, 2008, 07:39:59 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 :)