[英]REGEX: Capture Filename from URL without file extension
我正在尝试创建一个Javascript正则表达式,捕获没有文件扩展名的文件名。 我在这里阅读了其他帖子并且“转到此页面: http : //gunblad3.blogspot.com/2008/05/uri-url-parsing.html ”似乎是默认答案。 这似乎不适合我。 所以这就是我试图让正则表达式工作的方式:
我能得到的最接近的是: /([^ /] )。\\ w $字符串' http://example.com/index.htm'exec ()中的哪一个会捕获/index.htm和index 。
我需要这个才能捕获索引 。
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
此表达式将收集所有不是斜杠的字符,这些字符是由扩展名和字符串结尾立即跟随(由于前瞻 )(或者换句话说,在最后一个斜杠之后的所有内容,直到扩展名)。
或者,你可以做到这一点,而不完全的正则表达式,通过寻找最后的位置/
最后.
使用lastIndexOf
并在这些点之间获取substring
:
var url = "http://example.com/index.htm";
var filename = url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf("."));
测试和工作,即使对于没有文件扩展名的页面。
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
哦,不要理实。
我没有发现任何答案足够强大。 这是我的解决方案。
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
为了适应原始问题,默认行为是排除扩展名,但可以轻松撤消。
你可以尝试这个正则表达式:
([^/]*)\.[^.]*$
试试这个正则表达式。 它甚至可以处理具有多个句点的文件名。
(?<=\/)[^\/]*(?=\.\w+$)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.