代码之家  ›  专栏  ›  技术社区  ›  om_jaipur Guru

如何中断图像路径以只返回图像名称而不返回扩展名

  •  0
  • om_jaipur Guru  · 技术社区  · 7 年前

    我有一个图像的网址 "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

    2 回复  |  直到 7 年前
        1
  •  0
  •   Pushpesh Kumar Rajwanshi    7 年前

    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]);