Get the file name with the suffix in the URL

Wrote a method to get the file name after the url address

URL address:'https://img-operation.csdnimg.cn/csdn/silkroad/img/1605098486155.png'

Code:

var url = 'https://img-operation.csdnimg.cn/csdn/silkroad/img/1605098486155.png'

// 获取url中需要的数据  type  1: 获取文件名  2:获取后缀  3:获取文件名+后缀  4:获取文件前缀
function urlDemo(url, type) {
    let filename = url.substring(url.lastIndexOf('/') + 1)
    switch (type){
        case 1: return filename; break;
        case 2: return filename.substring(filename.lastIndexOf(".") + 1); break;
        case 3: return filename.substring(0, filename.lastIndexOf(".")); break;
        case 4: return url.substring(0, url.lastIndexOf('/') + 1)
    }   
}

console.log(urlDemo(url, 1))
console.log(urlDemo(url, 2))
console.log(urlDemo(url, 3))
console.log(urlDemo(url, 4))

effect:
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_36171287/article/details/112240901