Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quick regexp to get path

I need to extract the full path to a file using regExp

mydomain.com/path/to/file/myfile.html -> mydomain.com/path/to/file/

/mypath/file.txt -> /mypath/

anyone?

like image 865
David Hellsing Avatar asked Dec 23 '22 06:12

David Hellsing


1 Answers

Should be faster without RegExp:

path.substr(0, path.lastIndexOf('/') + 1);

Examples:

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);
""
like image 94
Vjeux Avatar answered Jan 02 '23 22:01

Vjeux