-- 创造无限可能

js技巧:获取文件后缀名方法

2022-10-20 13:07:59
969 人浏览 11 人点赞
有用,点赞支持一下

js技巧:获取文件后缀名方法

方法一:按.号切割字符串,获取最有一个元素

getSuffixName(file_path = ''){
    if(!file_path){
        return '';
    }
    let arr = file_path.split('.');
    return arr.length > 1?arr[arr.length - 1]:'';
}

方法二:按.号切割字符串,获取最有一个元素

getSuffixName(file_path = ''){
    if(!file_path){
        return '';
    }
    return file_path.split('.').pop();
}

方法三:获取最后一个.号位置,截取该位置最后的字符串

getSuffixName(file_path = ''){
    if(!file_path){
        return '';
    }
    return file_path.substring(file_path.lastIndexOf('.') + 1);
}