【问题标题】:REGEX: Capture Filename from URL without file extension正则表达式:从没有文件扩展名的 URL 捕获文件名
【发布时间】:2011-04-09 22:46:10
【问题描述】:

我正在尝试创建一个 Javascript 正则表达式来捕获没有文件扩展名的文件名。我已阅读此处的其他帖子,'转到此页面: http://gunblad3.blogspot.com/2008/05/uri-url-parsing.html' 似乎是默认答案。这似乎不适合我。所以这就是我试图让正则表达式工作的方法:

  1. 在主题字符串中查找最后一个正斜杠“/”。
  2. 捕获该斜线和下一个句点之间的所有内容。

我能得到的最接近的是:/([^/])。\w$字符串 'http://example.com/index.htm' > exec() 将捕获 /index.htm 和 index。

我只需要这个来捕获 index。

【问题讨论】:

    标签: javascript regex url


    【解决方案1】:
    var url = "http://example.com/index.htm";
    var filename = url.match(/([^\/]+)(?=\.\w+$)/)[0];
    

    我们来看看正则表达式:

    [^\/]+    # one or more character that isn't a slash
    (?=       # open a positive lookahead assertion
      \.      # a literal dot character
      \w+     # one or more word characters
      $       # end of string boundary
    )         # end of the lookahead
    

    这个表达式将收集所有不是斜线的字符,这些字符紧跟在扩展名和字符串末尾(感谢lookahead) - 或者换句话说,最后一个斜线之后的所有内容和直到扩展。

    或者,您可以完全不使用正则表达式来执行此操作,方法是使用 lastIndexOf 查找最后一个 / 和最后一个 . 的位置,并在这些点之间获得一个 substring:

    var url = "http://example.com/index.htm";
    var filename = url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf("."));
    

    【讨论】:

    • 此解决方案在包含多个句点的文件名上失败,如果您需要该测试,请参阅@BGerrissen 的解决方案。
    【解决方案2】:

    经过测试并且可以正常工作,即使对于没有文件扩展名的页面也是如此。

    var re = /([\w\d_-]*)\.?[^\\\/]*$/i;
    
    var url = "http://stackoverflow.com/questions/3671522/regex-capture-filename-from-url-without-file-extention";
    alert(url.match(re)[1]); // 'regex-capture-filename-from-url-without-file-extention'
    
    url = 'http://gunblad3.blogspot.com/2008/05/uri-url-parsing.html';
    alert(url.match(re)[1]); // 'uri-url-parsing'
    

    ([\w\d_-]*) 得到一个包含字母、数字、下划线或连字符的字符串。
    \.? 字符串后面可能跟一个句点。
    [^\\\/]*$ 但肯定后面不能跟斜杠或反斜杠,直到非常结束。
    /i 哦,是的,忽略大小写。

    【讨论】:

    • 这也捕获具有多个句点的文件名,接受的答案在这些文件名上失败。 (foo.global.js 等)。
    【解决方案3】:

    我没有找到任何足够可靠的答案。这是我的解决方案。

    function getFileName(url, includeExtension) {
        var matches = url && typeof url.match === "function" && url.match(/\/?([^/.]*)\.?([^/]*)$/);
        if (!matches)
            return null;
    
        if (includeExtension && matches.length > 2 && matches[2]) {
            return matches.slice(1).join(".");
        }
        return matches[1];
    }
    
    var url = "http://example.com/index.htm";
    var filename = getFileName(url);
    // index
    filename = getFileName(url, true);
    // index.htm
    
    url = "index.htm";
    filename = getFileName(url);
    // index
    filename = getFileName(url, true);
    // index.htm
    
    // BGerrissen's examples
    url = "http://stackoverflow.com/questions/3671522/regex-capture-filename-from-url-without-file-extention";
    filename = getFileName(url);
    // regex-capture-filename-from-url-without-file-extention
    filename = getFileName(url, true);
    // regex-capture-filename-from-url-without-file-extention
    
    url = "http://gunblad3.blogspot.com/2008/05/uri-url-parsing.html";
    filename = getFileName(url);
    // uri-url-parsing
    filename = getFileName(url, true);
    // uri-url-parsing.html
    
    // BGerrissen fails
    url = "http://gunblad3.blogspot.com/2008/05/uri%20url-parsing.html";
    filename = getFileName(url);
    // uri%20url-parsing
    filename = getFileName(url, true);
    // uri%20url-parsing.html
    
    // George Pantazis multiple dots
    url = "http://gunblad3.blogspot.com/2008/05/foo.global.js";
    filename = getFileName(url);
    // foo
    filename = getFileName(url, true);
    // foo.global.js
    
    // Fringe cases
    url = {};
    filename = getFileName(url);
    // null
    url = null;
    filename = getFileName(url);
    // null
    

    为了符合原始问题,默认行为是排除扩展名,但这很容易反转。

    【讨论】:

      【解决方案4】:

      你可以试试这个正则表达式:

      ([^/]*)\.[^.]*$
      

      【讨论】:

        【解决方案5】:

        试试这个正则表达式。它甚至可以处理带有多个句点的文件名。

        (?<=\/)[^\/]*(?=\.\w+$)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-18
          • 1970-01-01
          • 2010-10-12
          • 2010-09-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多