简体   繁体   English

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

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

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.我到处都在寻找一种仅使用 JS 删除路径的文件名的方法,但我只找到有关如何删除路径并保留文件名的信息。 I have this path:我有这条路:

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\1234567890.zip

But then, i only need the path:但是,我只需要路径:

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

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(/^.*[\\\/]/, '')

Extract with this regular expression:用这个正则表达式提取:

 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.") }

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

PATTERN EXPLANATION模式说明

--------------------------------------------------------------------------------
  ^                        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