在JavaScript中,处理URL和提取相关信息是一项常见任务。我们可以使用URLSearchParams接口轻松截取图片地址后面的查询参数,通过split()和pop()方法从路径中提取文件名,以及使用split()和slice()组合来截取文件后缀名。这些技巧使我们能够方便地操作URL和文件路径,从而在处理图片和其他资源时更加高效和灵活。您好,我是肥...
JS文件名和路径截取分割 常用方法有两种 以/Users/lz/project/test.txt为例分别介绍一下 方法一、 fullPath = '/Users/lz/project/test.txt'; pos = fullPath.lastIndexOf('/'); fileName = fullPath.substr(pos+1); console.log(fileName); filePath = fullPath.substr(0,pos); console.log(filePat...
[0:"https:"1:""2:"www.w3h5.com"3:"zb_users"4:"upload"5:"2019"6:"04"7:"201904031554286068121005.png"] 数组的最后一个就是文件名: 代码语言:javascript 复制 console.log(file[file.length-1]); 打印结果: 代码语言:javascript 复制 201904031554286068121005.png 这样就把图片的名称和后缀获取到了。
constname="http://localhost:8000/img/图片.jpg"// '/'所在的最后位置last=name.lastIndexOf('/')// 截取文件名称和后缀str=name.substring(last+1)// 输出:图片.jpg// 截取路径字符串url=name.substring(0,last)// 输出:http://localhost:8000/img// 截取文件名document=str.substring(0,str.lastInde...
constname ="http://localhost:8000/img/图片.jpg"// '/'所在的最后位置last = name.lastIndexOf('/')// 截取文件名称和后缀str = name.substring(last+1)// 输出:图片.jpg// 截取路径字符串url = name.substring(0,last)// 输出:http://localhost:8000/img// 截取文件名document= str.substring(0...