...

View Full Version : strip string to certain length



JayStang
06-28-2006, 04:15 PM
i have a string that has the path of an image

var str = "/somewhere/somewhere/image.jpg"


how would i go about stripping that to only display "image.jpg" keep in mind that i will be using different images from different locations so i can't just do a right(str, 9) because this will be different each time. I need more of a way to select the right until the first "/"

Thanks,
~Jamie

Mongus
06-28-2006, 04:33 PM
var filename = str.replace(/.*\/(.*)/,'$1'));

JayStang
06-28-2006, 04:52 PM
Hi Mongus,

Thanks for the reply.

That didn't seem to work for me. I'm new to javascript so could your or anyone else explain to me what that does?

Thanks,
~Jamie

Kor
06-28-2006, 04:59 PM
var filename = str.replace(/.*\/(.*)/,'$1');

mistyped an extra bracket

Kor
06-28-2006, 05:04 PM
<script type="text/javascript">
var str='/somewhere/somewhere/image.jpg'
var filename = str.replace(/.*\/(.*)/,'$1');
onload=function(){
alert(filename)
}
</script>

JayStang
06-28-2006, 05:51 PM
thank you

one last question... what if it was

var str='\somewhere\somewhere\image.jpg'

with "\" instead of "/"
??

Mongus
06-28-2006, 05:55 PM
Thanks KOR.

I copied from my test code and forgot to remove the trailing parenthesis. :o

Mongus
06-28-2006, 05:56 PM
This should check for either forward or back slashes.

str.replace(/.*[\/\\](.*)/,'$1');



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum