LoginSignup
82

More than 5 years have passed since last update.

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

Last updated at Posted at 2013-07-11

これは便利。

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さんのご指摘ありがとうございます!

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
82