JS: Most optimized way to remove a filename from a path in a string?
If you're using Node.js:
const path = require("path")
const removeFilePart = dirname => path.parse(dirname).dir
removeFilePart("/a/b/c/d.txt")
// Returns "/a/b/c"
Use lastIndexOf() to find the position of the last slash and get the part before the slash with substring().
str.substring(0, str.lastIndexOf("/"));