Jin*_*ong 280 javascript
有没有办法可以从完整路径获取最后一个值(基于'\'符号)?
例:
C:\Documents and Settings\img\recycled log.jpg
在这种情况下,我只想recycled log.jpg从JavaScript的完整路径中获取.
nic*_*ckf 647
var filename = fullPath.replace(/^.*[\\\/]/, '')
Run Code Online (Sandbox Code Playgroud)
这将处理\ OR/in路径
Dan*_*npe 98
仅仅为了表现,我测试了这里给出的所有答案:
var substringTest = function (str) {
return str.substring(str.lastIndexOf('/')+1);
}
var replaceTest = function (str) {
return str.replace(/^.*(\\|\/|\:)/, '');
}
var execTest = function (str) {
return /([^\\]+)$/.exec(str)[1];
}
var splitTest = function (str) {
return str.split('\\').pop().split('/').pop();
}
substringTest took 0.09508600000000023ms
replaceTest took 0.049203000000000004ms
execTest took 0.04859899999999939ms
splitTest took 0.02505500000000005ms
Run Code Online (Sandbox Code Playgroud)
获胜者是Split和Pop风格的答案,感谢bobince!
Rob*_*ach 67
在Node.js中,您可以使用Path的解析模块 ......
var path = require('path');
var file = '/home/user/dir/file.txt';
var filename = path.parse(file).base;
//=> 'file.txt'
Run Code Online (Sandbox Code Playgroud)
bob*_*nce 60
路径来自哪个平台?Windows路径不同于POSIX路径不同于Mac OS 9路径不同于RISC OS路径不同...
如果它是一个Web应用程序,其中文件名可以来自不同的平台,则没有一个解决方案.然而,合理的尝试是使用'\'(Windows)和'/'(Linux/Unix/Mac以及Windows上的替代方案)作为路径分隔符.这是一个非RegExp版本,可以带来额外的乐趣:
var leafname= pathname.split('\\').pop().split('/').pop();
Run Code Online (Sandbox Code Playgroud)
小智 28
Ates,您的解决方案不能防止空字符串作为输入.在那种情况下,它失败了TypeError: /([^(\\|\/|\:)]+)$/.exec(fullPath) has no properties.
bobince,这是一个处理DOS,POSIX和HFS路径分隔符(和空字符串)的nickf版本:
return fullPath.replace(/^.*(\\|\/|\:)/, '');
Run Code Online (Sandbox Code Playgroud)
小智 16
以下JavaScript代码行将为您提供文件名.
var z = location.pathname.substring(location.pathname.lastIndexOf('/')+1);
alert(z);
Run Code Online (Sandbox Code Playgroud)
Seb*_*mon 12
\xe2\x80\x99s不需要专门处理反斜杠;大多数答案不\xe2\x80\x99t 处理搜索参数。
\n现代方法是简单地使用URLAPI并获取pathname属性。API 将反斜杠规范化为斜杠。请注意,location(在浏览器环境中)也适用,但仅适用于当前URL,不适用于任意 URL。
为了将结果解析%20为空格,只需将其传递给decodeURIComponent.
const getFileName = (fileName) => new URL(fileName).pathname.split("/").pop();\n\n// URLs need to have the scheme portion, e.g. `file://` or `https://`.\nconsole.log(getFileName("file://C:\\\\Documents and Settings\\\\img\\\\recycled log.jpg")); // "recycled%20log.jpg"\nconsole.log(decodeURIComponent(getFileName("file://C:\\\\Documents and Settings\\\\img\\\\recycled log.jpg"))); // "recycled log.jpg"\nconsole.log(getFileName("https://example.com:443/path/to/file.png?size=480")); // "file.png"Run Code Online (Sandbox Code Playgroud)\r\n.as-console-wrapper { max-height: 100% !important; top: 0; }Run Code Online (Sandbox Code Playgroud)\r\n如果您始终想要路径的最后一个非空部分(例如from ),请.filter(Boolean)在 之前添加一个。.pop()file.pnghttps://example.com/file.png/
如果您只有相对 URL 但仍只想获取文件名,请使用构造函数的第二个参数URL来传递基本原点。"https://example.com"就足够了:new URL(fileName, "https://example.com"). \xe2\x80\x99s 也可以添加"https://"到你的fileName\xe2\x80\x8a\xe2\x80\x94\xe2\x80\x8aURL构造函数接受https://path/to/file.ext作为有效的 URL。
Sam*_*ton 12
在 Node.js 中,您可以使用该path.basename方法
const path = require('path');
const file = '/home/user/dir/file.txt';
const filename = path.basename(file);
//=> 'file.txt'
Run Code Online (Sandbox Code Playgroud)
Ate*_*ral 10
并不比nickf的答案更简洁,但是这个直接"提取"答案而不是用空字符串替换不需要的部分:
var filename = /([^\\]+)$/.exec(fullPath)[1];
Run Code Online (Sandbox Code Playgroud)
小智 9
另一个
var filename = fullPath.split(/[\\\/]/).pop();
Run Code Online (Sandbox Code Playgroud)
这里split有一个带有字符类
的正则表达式这两个字符必须用 '\' 转义
或者使用数组拆分
var filename = fullPath.split(['/','\\']).pop();
Run Code Online (Sandbox Code Playgroud)
如果需要,这将是将更多分隔符动态推送到数组中的方法。
如果fullPath由代码中的字符串显式设置,则需要转义反斜杠!
喜欢"C:\\Documents and Settings\\img\\recycled log.jpg"
小智 8
询问"获取没有扩展名的文件名"的问题请参考此处,但没有解决方法.以下是Bobbie解决方案修改的解决方案.
var name_without_ext = (file_name.split('\\').pop().split('/').pop().split('.'))[0];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
371209 次 |
| 最近记录: |