我需要的完整路徑提取使用正則表達式快速正則表達式來獲得路徑
mydomain.com/path/to/file/myfile.html
文件 - >mydomain.com/path/to/file/
/mypath/file.txt
- >/mypath/
的人?
我需要的完整路徑提取使用正則表達式快速正則表達式來獲得路徑
mydomain.com/path/to/file/myfile.html
文件 - >mydomain.com/path/to/file/
/mypath/file.txt
- >/mypath/
的人?
試試這個:
"mydomain.com/path/to/file/myfile.html".replace(/[^\/]*$/, "")
"/path/file.txt".replace(/[^\/]*$/, "")
但是你也可以通過拆分做沒有正則表達式:
"mydomain.com/path/to/file/myfile.html".split("/").slice(0, -1).join("/")+"/"
"/path/file.txt".split("/").slice(0, -1).join("/")+"/"
這工作:
var file="mydomain.com/path/to/file/myfile.html";
var match=/^(.*?)[\w\d\.]+$/g.exec(file);
var path=match[1];
此正則表達式應該做的:
^(?:[^\/]*\/)*
應該快沒有正則表達式:
path.substr(0, path.lastIndexOf('/') + 1);
例子:
var path = "mydomain.com/path/to/file/myfile.html";
path.substr(0, path.lastIndexOf('/') + 1);
"mydomain.com/path/to/file/"
var path = "/mypath/file.txt";
path.substr(0, path.lastIndexOf('/') + 1);
"/mypath/"
var path = "file.txt";
path.substr(0, path.lastIndexOf('/') + 1);
""