2010-09-08 58 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 

爲了適應原始問題,默認行爲是排除擴展名,但這很容易被顛倒。

相關問題