views:

30

answers:

3

I am trying to figure out how to select with JS everything before Picture_1.png in this url:

"http://s3.amazonaws.com/hq-photo/development/system/photos/215/cropped_thumb/Picture_1.png"

so that the end result is this :

"http://s3.amazonaws.com/hq-photo/development/system/photos/215/cropped_thumb"
A: 

This is commonly called getting the "dirname". A good starting point is

http://phpjs.org/functions/dirname:388

There are likely other versions if you Google for "javascript dirname"

MPD
+5  A: 
var str = "http://s3.amazonaws.com/hq-photo/development/system/photos/215/cropped_thumb/Picture_1.png";
var trimmed = str.substr(0, str.lastIndexOf("/"));

Demo: http://jsfiddle.net/gAxPx/

Ender
Thanks Ender! So much
Trip
Remember to choose this as the answer.
Ed
Happy to help :)
Ender
@Ed, they make me wait 10 minutes with my hand over the mouse clicker..
Trip
A: 

You can also do it with regular expressions.

var text = "http://s3.amazonaws.com/hq-photo/development/system/photos/215/cropped_thumb/Picture_1.png";
var dirname = text.match(/(.+)\//)[1];
Ed