js function to get filename from url

前端 未结 19 2584
挽巷
挽巷 2020-11-30 03:01

I have a url like http://www.example.com/blah/th.html

I need a javascript function to give me the \'th\' value from that.

All my urls have the same format (2

19条回答
  •  一整个雨季
    2020-11-30 03:22

    Use the match function.

    function GetFilename(url)
    {
       if (url)
       {
          var m = url.toString().match(/.*\/(.+?)\./);
          if (m && m.length > 1)
          {
             return m[1];
          }
       }
       return "";
    }
    

提交回复
热议问题