Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
683 views
in Technique[技术] by (71.8m points)

javascript - REGEX: Capture Filename from URL without file extension

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://gunblad3.blogspot.com/2008/05/uri-url-parsing.html' seems to be the default answer. This doesn't seem to do the job for me. So here is how I'm trying to get the regex to work:

  1. Find the last forward slash '/' in the subject string.
  2. Capture everything between that slash and the next period.

The closest I could get was : /([^/]).w$ Which on the string 'http://example.com/index.htm' exec() would capture /index.htm and index.

I need this to only capture index.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
var url = "http://example.com/index.htm";
var filename = url.match(/([^/]+)(?=.w+$)/)[0];

Let's go through the regular expression:

[^/]+    # 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

This expression will collect all characters that aren't a slash that are immediately followed (thanks to the lookahead) by an extension and the end of the string -- or, in other words, everything after the last slash and until the extension.

Alternately, you can do this without regular expressions altogether, by finding the position of the last / and the last . using lastIndexOf and getting a substring between those points:

var url = "http://example.com/index.htm";
var filename = url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf("."));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...