Find base name in URL in Javascript

后端 未结 6 1405
刺人心
刺人心 2020-12-09 17:35

I want to extract the base name from a image URL in Javascript. Would somebody care to give me a a hand on the Regex?

The rule would be:

  • return ever

6条回答
  •  温柔的废话
    2020-12-09 17:46

    I wouldn't actually use a regex in this case, but simply lastIndexOf and substring. Something like

    function findBaseName(url) {
        var fileName = url.substring(url.lastIndexOf('/') + 1);
        var dot = fileName.lastIndexOf('.');
        return dot == -1 ? fileName : fileName.substring(0, dot);
    }
    

提交回复
热议问题