2

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.

1 Answer 1

4

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

The string will always contain a slash, because tha's what I have coded in the plugin. But never the less, a nice controll:)
Felix - Technically you'll want to add 1 to path.lastIndexOf('/') in order to get the result requested in the question. :o)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.