REGEX: Capture Filename from URL without file extension

前端 未结 5 1297
忘掉有多难
忘掉有多难 2020-12-01 05:40

I am trying to create a Javascript Regex that captures the filename without the file extension. I have read the other posts here and \'goto this page: http://gunbl

5条回答
  •  甜味超标
    2020-12-01 06:16

    tested and works, even for pages without file extension.

    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_-]*) get a string containing letters, digits, underscores or hyphens.
    \.? perhaps the string is followed by a period.
    [^\\\/]*$ but certainly not followed by a slash or backslash till the very end.
    /i oh yeh, ignore case.

提交回复
热议问题