views:

37

answers:

1

I need to remove everything after the last '/'.

Source: /uploads/images/image1.jpg
Result: /uploads/images/

How do I do this with regular expression? I've tried different solutions, but it's not working. I get NULL as a result.

+4  A: 

No regex needed:

path = path.substring(0, path.lastIndexOf('/') + 1);

Reference: lastIndexOf(), substring()

You might want to check beforehand, whether the string contains a slash:

var index = path.lastIndexOf('/');
if(index > -1) {
    path = path.substring(0, index + 1);
}
Felix Kling
The string will always contain a slash, because tha's what I have coded in the plugin. But never the less, a nice controll:)
Steven
Felix - Technically you'll want to add `1` to `path.lastIndexOf('/')` in order to get the result requested in the question. :o)
patrick dw
@patrick dw: Ups, you are right... Thanks!
Felix Kling