473,671 Members | 2,283 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getting a substring in javascript

Hi Folk

I have the following string:

http://www.myurl.com/folderbit/moerf...sdaf/01354.jpg

and I want to extract the filename from it.

Do you know how to do this, if I don't know the exact names of the folders,
etc...

Thank you.
Nicolaas

Feb 17 '06 #1
3 9868
windandwaves wrote:
Hi Folk

I have the following string:

http://www.myurl.com/folderbit/moerf...sdaf/01354.jpg

and I want to extract the filename from it.

Do you know how to do this, if I don't know the exact names of the
folders, etc...

Thank you.
Nicolaas


This is what I came up with. I use a generic replace function just because
I am a little lazy (who is not?)

function getimagename(f) {
f = f.substring(f.l astIndexOf('/'))
f = replace(f, ".jpg", "");
return f;
}

function replace(s, t, u) {
/* ** s string to be processed ** t token to be found and removed
** u token to be inserted */
i = s.indexOf(t);
r = "";
if (i == -1) return s;
r += s.substring(0,i ) + u;
if ( i + t.length < s.length) {
r += replace(s.subst ring(i + t.length, s.length), t, u);
}
return r;
}
Feb 17 '06 #2

windandwaves wrote:
This is what I came up with. I use a generic replace function just because
I am a little lazy (who is not?)

function getimagename(f) {
f = f.substring(f.l astIndexOf('/'))
f = replace(f, ".jpg", "");
return f;
}

function replace(s, t, u) {
/* ** s string to be processed ** t token to be found and removed
** u token to be inserted */
i = s.indexOf(t);
r = "";
if (i == -1) return s;
r += s.substring(0,i ) + u;
if ( i + t.length < s.length) {
r += replace(s.subst ring(i + t.length, s.length), t, u);
}
return r;
}

function nameFromPath( path )
{
var re=/\/([^\.\/]+\.[^\.\/]+)$/.exec( path );

return re ? re[1].replace(/\.[^\.]+/,'') : "" ;
}

alert(
nameFromPath('h ttp://www.myurl.com/folderbit/moerfiasdfsadf/sdaf/01354.jpg'))

Feb 18 '06 #3
JRS: In article <SR************ **********@news .xtra.co.nz>, dated Sat,
18 Feb 2006 10:02:35 remote, seen in news:comp.lang. javascript,
windandwaves <wi*********@co ldmail.com> posted :
I have the following string:

http://www.myurl.com/folderbit/moerf...sdaf/01354.jpg

and I want to extract the filename from it.


Some consider the filename to include the extension; some do not.

S = "http://www.myurl.com/folderbit/moerfiasdfsadf/sdaf/01354.jpg"

S = S.replace(/.+\/(.+)/, "$1") // 01354.jpg

S = S.replace(/.+\/(.+)\..+/, "$1") // 01354

The second presumes that there *is* a non-empty extension; this does
not, but returns only the filename proper :-

S = S.replace(/.+\/([^.]+).*/, "$1")

Consider also

S = S.match(/.+\/([^.]+).*/)[1]

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Feb 18 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3242
by: Mike | last post by:
I got the code below from an earlier post but I can't get it to work (I get an error on the "for (i=0; i<a.length; i++)" line) Anyone have code that works for cookies with keys? > Anyone got any code out there that will set and retrieve > cookie values that have the key/subKey structure? Using the code provided below, examine
6
9342
by: dennishancy | last post by:
Trying to debug someone else's javascript code. There is a line that looks like this: req = req.substring(req.length-2, req.length); >From what I can gather in their comments, they want to capture the last two characters of the string "req". I'm guessing that the above line won't do that, and they want to change it to
0
3708
by: ruju00 | last post by:
I am getting an error in Login() method of the following class FtpConnection public class FtpConnection { public class FtpException : Exception { public FtpException(string message) : base(message){} public FtpException(string message, Exception innerException) : base(message,innerException){}
4
8757
by: Daviso | last post by:
Hi. I hava an input like this <td><input name=numberA> </td> and a button with a javascript function in onclick event. I want to control that the first element of the value I have introduced in numberA will be different and make a conditional
1
1100
by: SteveB | last post by:
Hi everyone! I'm a complete newbie to JavaScript and I've been tasked to do something that is seemingly very simple, but I can't quite figure how to do it. Using JavaScript, I need to call a URL passing it some parameters (this part I've done), now this URL will return a result like the following... OK|4540c2aa19a4dddf6ca52c2233b87f31
1
9477
by: kamleshsharmadts | last post by:
I am using Ajax with struts in web application. from jsp i am calling a function of ajax.js onclick of a button. code of that call function which calling from jsp given as below:- onclick="retrieveURL('/application/home_page.do?processAction=ItinerarySearch','AfoHomeForm')" after clicking on button i am getting following error:- object does not support this property or method at statement (Which i have made it bold in js file)
4
4908
by: Jean-Guy Mouton | last post by:
Hello, I am teaching myself Dom scripting with Javascript. I do not understand how to get a substring. If I have strings in the form: "This is string number 1" How can I put "1" into a variable to use it elsewhere? I thought I could use the function split and create an array but I have different string lengths with a variable number of words... Can you please help me? Thank you.
3
9542
by: tshad | last post by:
I have a file that I converted from VB.Net to C# that works fine in VB.Net when I compile but not in C# using the same libraries. The error I am getting is: PageInit.cs(9,7): error CS0138: A using namespace directive can only be applied to namespaces; 'System.Web.HttpCookie' is a class not a namespace The code is:
4
2372
by: N00b13 | last post by:
I have a great JS menu but I have to update every page each time I want to change a link. Is there a way to store my links in a file and call it so i only change that file? (what I have tried so far has not worked, but then again my JS really never works :) ) my .js: if (typeof Scl == "undefined") { var Scl = {}; } (function()
0
8400
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8823
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8602
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8672
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7441
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4412
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2817
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2058
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.