Steven
Steven

Reputation: 19425

Need help with javascript reg ex - how to remove part of a string

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.

Upvotes: 2

Views: 86

Answers (1)

Felix Kling
Felix Kling

Reputation: 816262

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);
}

Upvotes: 4

Related Questions