2010-09-08 59 views
8

我正在尝试创建一个JavaScript正则表达式,它可以捕获没有文件扩展名的文件名。我已阅读其他帖子在这里和'转到此页:http://gunblad3.blogspot.com/2008/05/uri-url-parsing.html'似乎是默认的答案。这似乎并不适合我。所以这里是我想如何让正则表达式工作:REGEX:从URL中捕获文件名,但没有文件扩展名

  1. 查找主题字符串中的最后一个正斜杠'/'。
  2. 捕获该斜杠和下一个周期之间的所有内容。

我能得到的最接近的是:/([^ /] )\ W $其中的字符串'http://example.com/index.htm' EXEC()将捕获将/index.htm索引

我需要这个来捕获索引

回答

39
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(".")); 
+1

如果您需要该测试,请参阅@ BGerrissen的解决方案,此解决方案对包含多个句点的文件名失败。 – 2012-08-18 02:51:04

1

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

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

测试工作,甚至没有文件扩展名的网页

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哦叶,忽略大小写。

+0

这也会捕获具有多个句点的文件名,这些被接受的答案会失败。 (foo.global.js等)。 – 2012-08-18 02:50:17

0

我没有发现任何答案接近强壮。这是我的解决方案。

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 

为了适应原始问题,默认行为是排除扩展名,但这很容易被颠倒。

相关问题