我有一个图片网址"https://wallpaper.wiki/wp-content/uploads/2017/04/wallpaper.wiki-Free-Images-Background-PC-PIC-WPD006550.jpg"
我要删除基本路径(https://wallpaper.wiki/wp-content/uploads/2017/04/
)和文件扩展名(.jpg
),并且应该返回 wallpaper.wiki-Free-Images-Background-PC-PIC-WPD006550
答案 0 :(得分:0)
您可以使用此正则表达式并捕获组1的内容,该内容将仅在网址中选择文件名(不包括扩展名)
https?:\/\/.*\/([\w.-]+)\.\w+
说明:
https?:\/\/.*/ --> captures the url from start and stops at last occurrence of /
([\w.-]+) --> captures the filename
\.\w+ --> captures the dot and extension
演示,https://regex101.com/r/eralSI/1
这是一个实现相同功能的javascript代码,
var myString = "https://wallpaper.wiki/wp-content/uploads/2017/04/wallpaper.wiki-Free-Images-Background-PC-PIC-WPD006550.jpg";
var myRegexp = /https?:\/\/.*\/([\w.-]+)\.\w+/g;
var match = myRegexp.exec(myString);
console.log(match[1]);
答案 1 :(得分:-1)
您还可以使用此功能来获取不使用正则表达式的名称
function getname(url){
url = url.split("?")[0];
url= url.substring(url.lastIndexOf("/")+1)
return url.substring(0,url.lastIndexOf("."))
}
var fullurl = "https://wallpaper.wiki/wp-content/uploads/2017/04/wallpaper.wiki-Free-Images-Background-PC-PIC-WPD006550.jpg";
alert(
getname(fullurl)
)