0

I hope to get the path of a filename, just like get the result /storage/emulated/0/Pictures/ when I input the string /storage/emulated/0/Pictures/Photo-2017-02-17-10-08-38_2271.JPG

How can I do in javascript? Thanks!

1
  • try var path = filename.match(/(.*)[\/\\]/)[1]||''; Commented Mar 8, 2017 at 7:19

3 Answers 3

1

Try

var fullpath = "/storage/emulated/0/Pictures/Photo-2017-02-17-10-08-38_2271.JPG"
var pathWithoutFileName = fullpath.substr(0, fullpath.lastIndexOf('/') + 1);
Sign up to request clarification or add additional context in comments.

Comments

0
var x = "/storage/emulated/0/Pictures/Photo-2017-02-17-10-08-38_2271.JPG"
var path = x.substr(0,x.lastIndexOf('/'));

Comments

0
var fullpath = "/storage/emulated/0/Pictures/Photo-2017-02-17-10-08-38_2271.JPG"
var path = fullpath.substr(0, fullpath.lastIndexOf('/') + 1);

The +1 is to include the last forward slash.

2 Comments

replace your second line as fullpath.substr(0, fullpath.lastIndexOf('/') + 1);
@GovindaRajbhar thanks for the observation. Had forgotten to alter the bogus variable names.

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.