View Full Version : reg exp help
ecnarongi
05-20-2003, 03:20 PM
how do I just grab the name of the graphic from any line that looks like this:
C:\helpfolder\toy.gif or http://www.helpme.com/temp/toy.gif
the only thing I want is the "toy.gif". all help is appreciated, thanks.
beetle
05-20-2003, 04:49 PM
var path = "http://www.helpme.com/temp/toy.gif";
var file = path.match( /[^/]+$/ );
or - a non regex solution
var file = path.substring( path.lastIndexOf( "/" ) + 1 );
ecnarongi
05-20-2003, 05:08 PM
I haven't tried it but I think that will work thanks beetle :thumbsup:
ecnarongi
05-22-2003, 04:30 PM
Beetle I tried this and I get the entire string instead of just the name of the graphic. Did I do something wrong, I used your code exactly. all help is appreciated, thanks.
beetle
05-22-2003, 04:36 PM
I just tested it and it works fine. Here, try this bookmarklet
javascript:void(alert(("http://www.helpme.com/temp/toy.gif").match(/[^/]+$/)));
ecnarongi
05-22-2003, 05:28 PM
you are right that does work, so all I need now is the variable path to be in quotes
var path = "\'+document.form1.FILE1.value+\'";
because it seems to only work when your value is in quotes. the above doesn't work I've tried to get it to work but I am still sucking. thanks for your help so far
document.form1.FILE1 is an element on my page.
beetle
05-22-2003, 05:41 PM
Have you tried:
var file = document.form1.FILE1.value.match( /[^/]+$/ );
???
ecnarongi
05-22-2003, 05:54 PM
see that would work but I need to quote the selection. for instance.
if document.form1.FILE1 had http://www.helpme.com/temp/toy.gif in it
and I called our function with
var path = document.form1.FILE1.value
var file = path.match( /[^/]+$/ );
I would not get the correct value b/c the path needs to be quoted. That's why
var path = "http://www.helpme.com/temp/toy.gif"
works and my example doesn't. so now I need a way to put quotes around document.form1.FIEL1.value and this is where I have tried and failed. thanks a lot so far
beetle
05-22-2003, 06:11 PM
That makes absolutely no sense. The quotes aren't part of the string - or the value retrieved from a form element. They are just a string delimeter. I mean - if quotes were needed, then I'd have to do this
var path = "\"" + "http://www.helpme.com/temp/toy.gif" + "\"";
Which is just ridiculous. I promise you - the quotes are NOT the issue. See the revised bookmarklet as proof (it includes the trailing quote in the result)
javascript:void(alert(("\""+"http://www.helpme.com/temp/toy.gif"+"\"").match(/[^/]+$/)));
Why don't you link me to a page you're having problems with or post the code.
Here's a working example
<html>
<head>
<script type="text/javascript">
function getFileName()
{
var file = document.form1.FILE1.value.match( /[^/]+$/ );
alert( file );
}
</script>
</head>
<body>
<form name="form1">
<input type="text" name="FILE1" value="http://www.helpme.com/temp/toy.gif" />
<br />
<input type="button" value="Get File Name" onclick="getFileName()" />
</form>
</body>
</html>
ecnarongi
05-22-2003, 07:44 PM
ok this is my sample code
<HTML>
<head>
<script language="javascript">
function upload()
{
var path = document.form1.FILE1.value;
var file = path.match(/[^/]+$/);
alert(file);
}
</script>
</head>
<BODY BGCOLOR="#FFFFFF">
<FORM METHOD="POST" ENCTYPE="multipart/form-data" name="form1">
File 1:<INPUT TYPE=FILE NAME="FILE1">
Description 1:<INPUT TYPE=hidden NAME="DESCR1" value=""><BR>
File 2:<INPUT TYPE=FILE NAME="FILE2">
Description 2:<INPUT TYPE=TEXT NAME="DESCR2"><BR>
<INPUT TYPE=button VALUE="Upload!" onClick="upload()">
</FORM>
</BODY>
</HTML>
now when I click on the upload button I get the full path but if I change path (the variable) to a URL surrounded by quotes I get the correct output. so that's why I said I need quotes.
ecnarongi
05-23-2003, 03:11 PM
I know what the problem is your reg exp handles URLs http://www.helpme.com/toy.gif but not file paths C:\desktop\toy.gif.
Can you modify your reg exp to handle both? thanks, all help is appreciated.
beetle
05-23-2003, 03:38 PM
Of course - the slashes are different
var file = path.match(/[^/\\]+$/);
Next time I tell you "it's not the quotes" -- please believe me.
ecnarongi
05-23-2003, 03:47 PM
Thanks Beetle,
I was looking for answers, so that's why I said the quotes. I took what you said and I found the real problem, thanks a lot. I need to really work on my reg exp.
:thumbsup:
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.