How to get the filename from string-path in javascript?
Here is my code
var nameString = "/app/base/controllers/filename.js"; //this is the input path string
do something here to get only the filename
var name = ??? //this value should equal to filename.js
The filename is the last part of the URL from the last trailing slash. For example, if the URL is http://www.example.com/dir/file.html then file. html is the file name.
Try this:
var nameString = "/app/base/controllers/filename.js"; var filename = nameString.split("/").pop();
I don't know why you'd want to us a regex to do this. Surely the following would be sufficient:
var nameString = "/app/base/controllers/filename.js"; var nameArray = nameString.split('/'); var name = nameArray[nameArray.length - 1];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With