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)
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...
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 :)