我有一个图片的完整路径,我正在使用jQuery读取它,像这样:
$('img.my_image').attr('src');
然而我只想要文件名部分(即不包括完整路径)。
是否有内置函数可以做到这一点,或者只能使用正则表达式?
我有一个图片的完整路径,我正在使用jQuery读取它,像这样:
$('img.my_image').attr('src');
然而我只想要文件名部分(即不包括完整路径)。
是否有内置函数可以做到这一点,或者只能使用正则表达式?
var Filename= path.split('/').pop()
var fileNameIndex = yourstring.lastIndexOf("/") + 1;
var filename = yourstring.substr(fileNameIndex);
function getFileName(path) {
return path.match(/[-_\w]+[.][\w]+$/i)[0];
}
我发现一个更好的处理类Unix和类Windows路径字符串的版本。
第一点:
var unix_path = '/tmp/images/cat.jpg';
console.log(unix_path.replace(/^.*[\\\/]/, ''));
var win_path = 'c:\\temp\images\cat.jpg';
console.log(win_path.replace(/^.*[\\\/]/, ''));
输出结果将会是cat.jpg
第二个方法:(可能更快)
var unix_path = '/tmp/images/cat.jpg';
console.log(unix_path.split(/[\\\/]/).pop());
var win_path = 'c:\\temp\images\cat.jpg';
console.log(win_path.split(/[\\\/]/).pop());
var win_path = 'c:\\temp\\images\\cat.jpg';
。否则,JavaScript 将无法将其解释为反斜杠。 - user1404617function getFileNameFromPath(path) {
var ary = path.split("/");
return ary[ary.length - 1];
}
//获取图像源 var path=$('img.my_image').attr('src');
//分割URL并获取带有文件扩展名的文件名 var file=path.split('/').pop();
//删除扩展名,只保留文件名 var filename=file.split('.').shift();
除了已接受的答案外,这似乎是最快速且跨平台(Unix和Windows)的解决方案:
function basePath(path) {
return path.substr(
Math.max(
path.lastIndexOf('\\'),
path.lastIndexOf('/'),
) + 1,
);
}
当你有来自Unix和Windows的数据需要在一个地方解析时,这是必要的。
该函数只取所有可能分隔符的最新值,并返回最后一个分隔符后的字符串。对于大字符串,它比较快,原因如下:
Tested in Chromex87:
// Test subjects
//=====================
function basePathUnix(path) {
return path.split('/').pop();
}
function basePathUnix2(path) {
const arr = path.split('/');
return arr[ arr.length - 1 ];
}
function basePathUnix3(path) {
return path.substr(path.lastIndexOf('/') + 1);
}
function basePathCrossPlatform(path) {
return path.replace(/^.*[\\\/]/, '');
}
function basePathCrossPlatform2(path) {
return path.split(/[\\\/]/).pop();
}
function basePathCrossPlatform3(path) {
return path.substr(Math.max(path.lastIndexOf('\\'), path.lastIndexOf('/')) + 1);
}
function basePathCrossPlatform4(path, separators = ['/', '\\']) {
return path.substr(Math.max(...separators.map(s => path.lastIndexOf(s))) + 1);
}
// Tests
//=====================
function measureTime(name, fn) {
const start = window.performance.now();
for (let i = 0; i < 10000; i++) {
fn();
}
const time = window.performance.now() - start;
console.log(name, time);
}
function testResults(name, path) {
console.log('\n[CHECK RESULTS]', name);
console.log('basePathUnix:\t\t', basePathUnix(path));
console.log('basePathUnix2:\t\t', basePathUnix2(path));
console.log('basePathUnix3:\t\t', basePathUnix3(path));
console.log('basePathCrossPlatform:\t', basePathCrossPlatform(path));
console.log('basePathCrossPlatform2:\t', basePathCrossPlatform2(path));
console.log('basePathCrossPlatform3:\t', basePathCrossPlatform3(path));
console.log('basePathCrossPlatform4:\t', basePathCrossPlatform4(path));
}
function testPerformance(name, path) {
console.log('\n[MEASURE PERFORMANCE]', name);
measureTime('basePathUnix:\t\t', () => basePathUnix(path));
measureTime('basePathUnix2:\t\t', () => basePathUnix2(path));
measureTime('basePathUnix3:\t\t', () => basePathUnix3(path));
measureTime('basePathCrossPlatform:\t', () => basePathCrossPlatform(path));
measureTime('basePathCrossPlatform2:\t', () => basePathCrossPlatform2(path));
measureTime('basePathCrossPlatform3:\t', () => basePathCrossPlatform3(path));
measureTime('basePathCrossPlatform4:\t', () => basePathCrossPlatform4(path));
}
function runTest(name, path) {
setTimeout(() => {
testResults(name, path);
setTimeout(() => {
testPerformance(name, path);
}, 200);
}, 200);
}
// Run tests
//=====================
setTimeout(() => {
const pathUnix = '/some/path/string/some/path/string/some/path/string/some/path/string/some/path/string/some/path/file-name';
runTest('UNIX', pathUnix);
}, 1000);
setTimeout(() => {
const pathWind = '\\some\\path\\string\\some\\path\\string\\some\\path\\string\\some\\path\\string\\some\\path\\file-name';
runTest('WINDOWS', pathWind);
}, 2000);
const getFileName = filePath => filePath.replaceAll("\\", "/").split("/").pop();
getFileName("/a/b/c.txt");
// It will return "c.txt"
getFileName("\\a\\b\\c.txt"); // The "\\" is just to escape "\" char
// It will still return "c.txt"
这将支持Windows和非Windows操作系统。