##split 截取####
function getFileNameAndExtension(fileUrl) {
/**
* 从文件 URL 中提取文件名和后缀名
* @param {string} fileUrl - 完整的文件 URL
* @return {object} - 包含文件名和后缀名的对象
*/
// 获取文件名(不包括路径部分)
const fileName = fileUrl.substring(fileUrl.lastIndexOf('/') + 1);
// 提取文件后缀名
const fileExtension = fileName.match(/\.([a-zA-Z0-9]+)$/);
return {
fileName: fileName,
extension: fileExtension ? fileExtension[1] : ''
};
}
// 示例调用
const fileUrl = "http://images.eefaith.com/faith/dev/菲斯/default/20241218/1734487292586123.xlsx";
const { fileName, extension } = getFileNameAndExtension(fileUrl);
console.log(`文件名: ${fileName}, 后缀名: ${extension}`);
## 正则截取 ##
function getFileExtension(fileUrl) {
/**
* 从文件 URL 中提取文件后缀名
* @param {string} fileUrl - 完整的文件 URL
* @return {string} - 文件的后缀名
*/
const match = fileUrl.match(/\.([a-zA-Z0-9]+)$/);
if (match) {
return match[1];
}
return ''; // 如果没有找到后缀,返回空字符串
}
// 示例调用
const fileUrl = "http://images.eefaith.com/faith/dev/菲斯/default/20241218/1734487292586123.xlsx";
const extension = getFileExtension(fileUrl);
console.log(extension); // 输出:xlsx