【JS】在路径中截取文件名或后缀名

 使用subtring() 截取字符串

name = "http://localhost:8080/static/香菜.png"
pos = name.lastIndexOf('/')  // '/'所在的最后位置
str = name.substr(pos+1)  //截取文件名称和后缀   输出:香菜.png
url = name.substr(0,pos)  //截取路径字符串    输出:http://localhost:8080/static
pname = str.substring(0, str.lastIndexOf("."))  //截取文件名   输出:香菜
houzhui = name.substring(name.lastIndexOf("."))  //截取后缀   输出:.png
phouzhui = name.substring(name.lastIndexOf(".")+1)  //截取后缀名  输出:png

猜你喜欢

转载自blog.csdn.net/m0_62811051/article/details/128272809