admin管理员组文章数量:1129022
I have a url like .html
I need a javascript function to give me the 'th' value from that.
All my urls have the same format (2 letter filenames, with .html extension).
I want it to be a safe function, so if someone passes in an empty url it doesn't break.
I know how to check for length, but I should be checking for null to right?
I have a url like http://www.example.com/blah/th.html
I need a javascript function to give me the 'th' value from that.
All my urls have the same format (2 letter filenames, with .html extension).
I want it to be a safe function, so if someone passes in an empty url it doesn't break.
I know how to check for length, but I should be checking for null to right?
Share Improve this question asked Feb 4, 2009 at 15:11 BlankmanBlankman 267k330 gold badges795 silver badges1.2k bronze badges 2- 1 Possible duplicate of How to get the file name from a full path using JavaScript? – Liam Commented Aug 2, 2017 at 13:45
- For something as specific as you want, you may use RegEx: stackoverflow.com/a/73341035/7389293 – carloswm85 Commented Aug 13, 2022 at 1:36
26 Answers
Reset to default 233var filename = url.split('/').pop()
Why so difficult?
= url.split('#')[0].split('?')[0].split('/').pop();
RegEx below would result same as the above's normally, but will return empty string if the URL was significantly malformed.
= (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")
All three of them would return
本文标签:
javascriptjs function to get filename from urlStack Overflow
版权声明:本文标题:javascript - js function to get filename from url - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人,
转载请联系作者并注明出处:http://www.betaflare.com/web/1736732237a1950058.html,
本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
file
发表评论