Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript如何从文件名中分离路径_Javascript - Fatal编程技术网

JavaScript如何从文件名中分离路径

JavaScript如何从文件名中分离路径,javascript,Javascript,我需要将文件路径与文件名分开:(过去有些代码不起作用) 如果路径为www.example.com/hi/test/home.html 如何获得www.example.com/hi/test/?您可以使用子字符串和lastIndexOf: a = "www.example.com/hi/test/home.html"; b = a.substr(0, a.lastIndexOf('/')); // b = www.example.com/hi/test 查看split、pop和join方法,然

我需要将文件路径与文件名分开:(过去有些代码不起作用) 如果路径为www.example.com/hi/test/home.html
如何获得www.example.com/hi/test/

您可以使用
子字符串和
lastIndexOf

a = "www.example.com/hi/test/home.html";
b = a.substr(0, a.lastIndexOf('/'));

// b = www.example.com/hi/test

查看
split
pop
join
方法,然后尝试一些操作。您可以查看此线程的详细信息:
“www.example.com/hi/test/home.html”。子字符串(0,url.lastIndexOf(“/”)+1)我刚刚做了
var fullPath=path+“/”不要忘记变量前面的
var
s、
const
s或
let
s。:)获取尾部斜杠
url.substring(0,url.lastIndexOf(“/”)+1)