使用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+"\"");
Run Code Online (Sandbox Code Playgroud) 我很难获得这个bash脚本来执行输入的格式化.这很简单,但是当它执行以'newstring ='开头的行时,它不执行sed操作,它只打印我的输入(直到第一个空格)然后直接打印我的sed命令.我究竟做错了什么?
#! /bin/bash
##format paths/strings with spaces to escape the spaces with a forward-slash'\'
##then use 'open' to open finder at current-set directory (based on path)
oldstring="$1"
newstring="$oldstring | sed 's/ /\\ /g')"
cd $newstring
open .
Run Code Online (Sandbox Code Playgroud)