http://www.example.com/blah/th.html” http://www.example.com/blah/th.html。
我需要一种 j印功能,给我留下这种价值。
我的所有王位都采用相同的格式(2封信标,延长日期:html)。
我希望它是一种安全的职能,这样,如果有人用空洞的ur子走,那就不了。
我知道如何检查时间长,但我应该检查一下谁是谁?
http://www.example.com/blah/th.html” http://www.example.com/blah/th.html。
我需要一种 j印功能,给我留下这种价值。
我的所有王位都采用相同的格式(2封信标,延长日期:html)。
我希望它是一种安全的职能,这样,如果有人用空洞的ur子走,那就不了。
我知道如何检查时间长,但我应该检查一下谁是谁?
var filename = url.split( / ).pop()
为什么如此困难?
= url.split( # )[0].split( ? )[0].split( / ).pop();
下面的登记结果与上述通常相同,但如果URL明显畸形,则会退回空洞。
= (url.match(/^w+:(/+([^/#?s]+)){2,}(#|?|$)/)||[])[2]|| ;
// Returns empty string for relative URLs unlike the original approach
= (url.match(/^w+:(/+([^/#?s]+)){2,}/)||[])[2]|| ;
// Ignores trailing slash (e.g., ".../posts/?a#b" results "posts")
这三个国家将返回<代码>. ?
my://host/path=&%dir///file?.name?q=1/2&3=&#a/?b//?
第三种办法还将从下列地点退回<代码>dir. 姓名:
my://host/path=&%dir///dir.name/?not/these/#/ones//?
First Edit (Sep 2023) :
测试your URL,加入如下。 第三个问题将忽视上述法典描述的拖拉。 请评论RegEx公司是否适当处理任何有效的URL
<style>*{margin:0;padding:0;box-sizing:border-box;border:none;}label{font-size:90%}input{display:block;margin:.2em auto 0;padding:.8em 1.2em;width:calc(100% - 5px);background:#f8f8f8;border-radius:1em;}p{margin-top:1.4em;padding-left:.5em;font-size:95%;color:#0008}p::after{content:attr(title);margin-left:.5em;padding:.4em .2em;background:#fe8;border-radius:.5em;font-weight:800;color:#000;}</style>
<label for="input">Enter your URL here:</label>
<input value="my://host/path=&%dir///file?.name?q=1/2&3=&#a/?b//?" id="input">
<p>A</p>
<p>B</p>
<p>C</p>
<script>
((input = document.querySelector( input ), output = document.querySelectorAll( p )) => {
const getFilename = _=> {
output[0].title = input.value.split( # )[0].split( ? )[0].split( / ).pop();
output[1].title = (input.value.match(/^w+:(/+([^/#?s]+)){2,}(#|?|$)/)||[])[2]|| ;
output[2].title = (input.value.match(/^w+:(/+([^/#?s]+)){2,}/)||[])[2]|| ;
};
getFilename();
input.addEventListener( input , getFilename);
input.select();
})();
</script>
使用配对功能。
function GetFilename(url)
{
if (url)
{
var m = url.toString().match(/.*/(.+?)./);
if (m && m.length > 1)
{
return m[1];
}
}
return "";
}
Similar to the others, but...I ve used Tom s simple script - a single line,
then you can use the filename var anywhere:
http://www.tomhoppe.com/index.php/2008/02/grab-filename-from-window-location/
var filename = location.pathname.substr(location.pathname.lastIndexOf("/")+1);
除现有答复外,我建议使用URL() 构造/code>。 (browsers and Node.js) 因为你可以肯定你的URL是有效的:
const url = https://test.com/path/photo123.png?param1=1¶m2=2#hash ;
let filename = ;
try {
filename = new URL(url).pathname.split( / ).pop();
} catch (e) {
console.error(e);
}
console.log(`filename: ${filename}`);
这应当适用于所有案件。
function getFilenameFromUrl(url) {
const pathname = new URL(url).pathname;
const index = pathname.lastIndexOf( / );
return pathname.substring(index + 1) // if index === -1 then index+1 will be 0
}
用于URL查询和洗衣识别剂的常规解决办法:
function fileNameFromUrl(url) {
var matches = url.match(//([^/?#]+)[^/]*$/);
if (matches.length > 1) {
return matches[1];
}
return null;
}
JSFiddle here。
由于案件往往与海关编码不一致,因此,I考察了Javad 字典:URL。 Alas, It chokes on relative URLs! 而且,它没有获得档案名称的财产。
<<>has>/em> 是解决这一共同问题的良好图书馆。 Behold URI.js。 你们所需要的是一份简单的声明,如:
let file = new URI(url).filename()
这样,我们就能够形成一种简单的职能,从而取消检查,并取消档案延期:
function fileName(url) {
if (url === null || typeof url === undefined )
return
let file = new URI(url).filename() // File name with file extension
return file.substring(0, file.lastIndexOf( . )) // Remove the extension
}
这里有一刀子,有测试案例可以与周围一起进行。 除驾驶路线外,所有案件都通过。
test( Dots in file name without URL , dotted.file.name.png , dotted.file.name )
test( Dots in file name with URL , http://example.com/file.name.txt , file.name )
test( Lengthy URL with parameters , /my/folder/filename.html#dssddsdsd?toto=33&dududu=podpodpo , filename )
test( URL with hash , /my/folder/filename.html#dssddsdsd , filename )
test( URL with query strings , /my/folder/filename.html?toto=33&dududu=podpodp , filename )
test( Hash after query string , http://www.myblog.com/filename.php?year=2019#06 , filename )
test( Query parameter with file path character , http://www.example.com/filename.zip?passkey=1/2 , filename )
test( Query parameter with file path character and hash , http://www.example.com/filename.html?lang=en&user=Aan9u/o8ai#top , filename )
test( Asian characters , http://example.com/文件名.html , 文件名 )
test( URL without file name , http://www.example.com , )
test( Null , null, )
test( Undefined , undefined, )
test( Empty string , , )
test( Drive path name , C:/fakepath/filename.csv , filename )
function fileName(url) {
if (url === null || typeof url === undefined )
return
let file = new URI(url).filename() // File name with file extension
return file.substring(0, file.lastIndexOf( . )) // Remove the extension
}
function test(description, input, expected) {
let result = fileName(input)
let pass = FAIL
if (result === expected)
pass = PASS
console.log(pass + : + description + : + input)
console.log( => " + fileName(input) + " )
}
<script src="https://cdn.jsdelivr.net/gh/medialize/URI.js@master/src/URI.js"></script>
<
PASS: Dots in file name without URL: dotted.file.name.png
=> "dotted.file.name"
PASS: Dots in file name with URL: http://example.com/file.name.txt
=> "file.name"
PASS: Lengthy URL with parameters: /my/folder/filename.html#dssddsdsd?toto=33&dududu=podpodpo
=> "filename"
PASS: URL with hash: /my/folder/filename.html#dssddsdsd
=> "filename"
PASS: URL with query strings: /my/folder/filename.html?toto=33&dududu=podpodp
=> "filename"
PASS: Hash after query string: http://www.myblog.com/filename.php?year=2019#06
=> "filename"
PASS: Query parameter with file path character: http://www.example.com/filename.zip?passkey=1/2
=> "filename"
PASS: Query parameter with file path character and hash: http://www.example.com/filename.html?lang=en&user=Aan9u/o8ai#top
=> "filename"
PASS: Asian characters: http://example.com/文件名.html
=> "文件名"
PASS: URL without file name: http://www.example.com
=> ""
PASS: Null: null
=> ""
PASS: Undefined: undefined
=> ""
PASS: Empty string:
=> ""
FAIL: Drive path name: C:/fakepath/filename.csv
=> ""
这一解决办法是,如果你重心写习俗法典,不要使用图书馆为你工作,那么你会这样做。 如果你想要找到解决办法,那是 t的。
those will not work for lenghty url like
"/my/folder/questions.html#dssddsdsd?toto=33&dududu=podpodpo"
here I expect to get "questions.html". So a possible (slow) solution is as below
fname=function(url)
{ return url?url.split( / ).pop().split( # ).shift().split( ? ).shift():null }
那么,你就可以检验,在任何情况下,你只能获得档案名称。
fname("/my/folder/questions.html#dssddsdsd?toto=33&dududu=podpodpo")
-->"questions.html"
fname("/my/folder/questions.html#dssddsdsd")
-->"questions.html"
fname("/my/folder/questions.html?toto=33&dududu=podpodpo")
"-->questions.html"
(and it works for null)
(I would love to see a faster or smarter solution)
这只是在浏览器环境中工作。 不适合噪音。
function getFilename(url) {
const filename = decodeURIComponent(new URL(url).pathname.split( / ).pop());
if (!filename) return index.html ; // some default filename
return filename;
}
function filenameWithoutExtension(filename) {
return filename.replace(/^(.+?)(?:.[^.]*)?$/, $1 );
}
这里有两个职能:
对URL进行校服时,最好选择新的<代码>URL。 并且注意到URL并不总是包含档案名称。
Notice: This function try to resolve filename from an URL. But it do NOT guarantee that the filename is valid and suitable for use:
:
in windows,