【发布时间】:2011-11-27 22:29:05
【问题描述】:
使用 Javascript 我想从字符串末尾删除文件名(路径+文件名),只留下目录路径。
正则表达式会是理想的吗?或者,有没有更简单的方法使用字符串对象来做到这一点?
感谢您的帮助!
----已回答和解释---
这段代码的目的是打开一个目录的finder。我能够提取的数据包括一个文件名 - 因为我只是试图打开 finder (mac) 到该位置,所以我需要删除文件名。这是我最终得到的结果:
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+"\"");
【问题讨论】:
标签: javascript regex string path