最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Best way to split URL into array in clean way? - Stack Overflow

programmeradmin8浏览0评论

I need to extract the directory and file name in a different input of user URL's.

Some examples would include:

  • https://foo/s3.amazonaws/TOP_PROD_IMAGE/WS-25612-BK_IMRO_1.jpg
  • http://192.168.12.44:8090/TOP_PROD_IMAGE/R3CRDT-HZWT_IMRO_1.jpg
  • www.foobar-images.s3.amazonaws/TOP_PROD_IMAGE/WS-25612-BK_IMRO_1.jpg

What I really need is the TOP_PROD_IMAGE and WS-25612-BK_IMRO_1.jpg file name.

So I would need to account for users who enter http:// or https:// or just www. so I tried using string.split('/') but that obviously wouldn't work in all cases. Is there something that could give me an array despite the double // in cases where user enters http? Thanks!

I need to extract the directory and file name in a different input of user URL's.

Some examples would include:

  • https://foo/s3.amazonaws./TOP_PROD_IMAGE/WS-25612-BK_IMRO_1.jpg
  • http://192.168.12.44:8090/TOP_PROD_IMAGE/R3CRDT-HZWT_IMRO_1.jpg
  • www.foobar-images.s3.amazonaws./TOP_PROD_IMAGE/WS-25612-BK_IMRO_1.jpg

What I really need is the TOP_PROD_IMAGE and WS-25612-BK_IMRO_1.jpg file name.

So I would need to account for users who enter http:// or https:// or just www. so I tried using string.split('/') but that obviously wouldn't work in all cases. Is there something that could give me an array despite the double // in cases where user enters http? Thanks!

Share Improve this question edited Dec 24, 2019 at 0:08 C.OG 6,5373 gold badges24 silver badges41 bronze badges asked Dec 23, 2019 at 23:02 rec0nstrrec0nstr 19510 bronze badges 3
  • I'd use path-to-regexp for this. it's used within Express internally, and can be quite robust. npmjs./package/path-to-regexp If this really is just a one-off use case though, you could do it directly with regex. – Brad Commented Dec 23, 2019 at 23:08
  • ([^/]+)\/([^/]+)$ as a regexp? – vsh Commented Dec 23, 2019 at 23:09
  • This question reminded me of a similar one here But this one should be a lot easier. – LukStorms Commented Dec 23, 2019 at 23:11
Add a ment  | 

4 Answers 4

Reset to default 6

Consider:

const [file, folder] = url.split('/').reverse();

With this you wouldn't need to consider http:// or any //

How about:

const url = new URL('https://foo/s3.amazonaws./TOP_PROD_IMAGE/WS-25612-BK_IMRO_1.jpg')
const urlParams = url.pathname.split('/') // you'll get array here, so inspect it and get last two items

Will this do the trick? You'll get exactly what you need within the pathname.

If the urls have to start with either http and optional s or www. you could also use a pattern with 2 capturing groups to get the part before the last slash and the part after the last slash.

^(?:https?:\/\/|www\.)\S+\/([^/]+)\/(\S+)$

Regex demo

urls = [
  "https://foo/s3.amazonaws./TOP_PROD_IMAGE/WS-25612-BK_IMRO_1.jpg",
  "http://192.168.12.44:8090/TOP_PROD_IMAGE/R3CRDT-HZWT_IMRO_1.jpg",
  "www.foobar-images.s3.amazonaws./TOP_PROD_IMAGE/WS-25612-BK_IMRO_1.jpg"
].forEach(s => {
  let m = s.match(/^(?:https?:\/\/|www\.)\S+\/([^/]+)\/(\S+)$/, s);
  console.log(m[1]);
  console.log(m[2]);
  console.log("\n");
});

You can use negative look-aheads to only match the final URI segments:

/(?!([https?:\/\/]|[www.]))(?!([\d]))(?!(.*[])).*/

const re = /(?!([https?:\/\/]|[www.]))(?!([\d]))(?!(.*[])).*/
const arr = [
  "https://foo/s3.amazonaws./TOP_PROD_IMAGE/WS-25612-BK_IMRO_1.jpg",
  "http://192.168.12.44:8090/TOP_PROD_IMAGE/R3CRDT-HZWT_IMRO_1.jpg",
  "www.foobar-images.s3.amazonaws./TOP_PROD_IMAGE/WS-25612-BK_IMRO_1.jpg"
]

const res = arr.map(str => re.exec(str)[0].split("/"))

console.log(res)

发布评论

评论列表(0)

  1. 暂无评论