我正在尝试创建一个 Javascript 正则表达式来捕获不带文件扩展名的文件名。我已经阅读了这里的其他帖子并且'转到这个页面: http://gunblad3.blogspot.com/2008/05/uri-url-parsing.html http://gunblad3.blogspot.com/2008/05/uri-url-parsing.html' 似乎是默认答案。这似乎不适合我。所以这就是我试图让正则表达式工作的方法:
- 找到主题字符串中的最后一个正斜杠“/”。
- 捕获该斜杠和下一个句点之间的所有内容。
我能得到的最接近的是:/([^/]).\w$弦上的哪个'http://example.com/index.htm http://example.com/index.htm'exec() 会捕获/index.htm and index.
我只需要这个来捕获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
该表达式将收集紧随其后的不是斜杠的所有字符(感谢展望 http://www.regular-expressions.info/lookaround.html) 由扩展名和字符串末尾组成——或者换句话说,是最后一个斜杠之后到扩展名之前的所有内容。
或者,您可以完全不使用正则表达式,通过查找最后一个的位置来完成此操作/
和最后一个.
using lastIndexOf http://www.w3schools.com/jsref/jsref_lastIndexOf.asp并得到一个substring http://www.w3schools.com/jsref/jsref_substring.asp在这些点之间:
var url = "http://example.com/index.htm";
var filename = url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf("."));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)