繁体   English   中英

如何从只有 JS 的路径中删除文件名?

[英]How to remove filename from a path with only JS?

我到处都在寻找一种仅使用 JS 删除路径的文件名的方法,但我只找到有关如何删除路径并保留文件名的信息。 我有这条路:

C:\Users\usuario\Desktop\CyT\Proyectos\AlmPas\2022-06-17\luis@gmail.com\1234567890.zip

但是,我只需要路径:

C:\Users\usuario\Desktop\CyT\Proyectos\AlmPas\2022-06-17\luis@gmail.com\

如果我使用下一个代码,我只得到文件名,但我怎样才能得到没有文件名的路径?

soloArchivo = ruta_archivos.replace(/^.*[\\\/]/, '')

用这个正则表达式提取:

 const str = String.raw`C:\Users\usuario\Desktop\CyT\Proyectos\AlmPas\2022-06-17\luis@gmail.com\1234567890.zip` const regex = /^.*[\\\/]/; const match = str.match(regex); if (match) { console.log(match[0]) } else { console.log("Sorry, there is no match.") }

结果C:\Users\usuario\Desktop\CyT\Proyectos\AlmPas\2022-06-17\luis@gmail.com\

模式说明

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  [\/]                     any character of: '\/'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM