Help us understand the problem. What is going on with this article?

URLからファイル名を取得する正規表現

More than 5 years have passed since last update.

これは便利。

var url = window.location.href;

// 拡張子付きで
var filename_ex = url.match(".+/(.+?)([\?#;].*)?$")[1];
// 拡張子無しで
var filename = url.match(".+/(.+?)\.[a-z]+([\?#;].*)?$")[1];

参考
http://javascript-memo2.seesaa.net/article/159034796.html

※2013.7.11
参考URLのスクリプトでは、QueryStringやhashがあった場合、ファイル名が正常に取得出来ないの、修正しました。kawazさんのご指摘ありがとうございます!

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away