Using Javascript I'd like to remove the filename from the end of a string (path+filename), leaving me just the directory path.

Would regular expressions be ideal? Or, is there a simpler means of doing this using the string object?

Thanks for any help!

----ANSWERED & EXPLAINED---

The purpose of this code was to open finder to a directory. The data I was able to extract included a filename - since I was only trying to open finder (mac) to the location, I needed to strip the filename. Here's what I ended up with:

var theLayer = app.project.activeItem.selectedLayers[0];
//get the full path to the selected file
var theSpot = theLayer.source.file.fsName;
//strip filename from the path
var r = /[^\/]*$/;
var dirOnly = theSpot.replace(r, '');
//use 'system' to open via shell in finder
popen = "open"
var runit = system.callSystem(popen+" "+"\""+dirOnly+"\"");

As noted in this answer, if you're using node.js (fair assumption if you're dealing with file paths) - you can use the path module, call path.parse, and retrieve the directory name with dir like this:

const path = require("path")

let myPath = "folder/path/file.txt"
let myDir = path.parse(myPath).dir

console.log(myDir) // "folder/path"

This should be the most robust way to manage and parse file paths across different environments.