如何仅使用JS从路径中删除文件名?

发布于 2025-02-08 14:22:02 字数 781 浏览 1 评论 0 原文

我到处都在寻找一种仅通过JS删除路径的文件名的方式,但是我只找到了如何删除路径并保留文件名的信息。 我有这条路:

c:\ users \ usuario \ desktop \ cyt \ proyectos \ almpas \ almpas \ 2022-06-17 \ [email  pretanded] \ 1234567890.zip

但是,我只需要路径:

c:\ users \ usuario \ desktop \ cyt \ proyectos \ almpas \ almpas \ 2022-06-17 \ [email  prectioned] \

如果使用下一个代码,我只会获得文件名,但是如果没有文件名,我该如何获得路径?

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

i was looking everywhere to a way of remove only the filename of a path with JS, but I only find information of how to remove the path, and keep the filename.
I have this path:

C:\Users\usuario\Desktop\CyT\Proyectos\AlmPas\2022-06-17\[email protected]\1234567890.zip

But then, i only need the path:

C:\Users\usuario\Desktop\CyT\Proyectos\AlmPas\2022-06-17\[email protected]\

If i use the next code, i get the filename only, but how can i get the path without the filename?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薔薇婲 2025-02-15 14:22:02

提取此正则表达式:

const str = String.raw`C:\Users\usuario\Desktop\CyT\Proyectos\AlmPas\2022-06-17\[email protected]\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 \ preyectos \ almpas \ almpas \ 2022-06-17 \ [email  procearted] \

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

Extract with this regular expression:

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

Results: C:\Users\usuario\Desktop\CyT\Proyectos\AlmPas\2022-06-17\[email protected]\

PATTERN EXPLANATION

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  [\/]                     any character of: '\/'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文