Q:

Write a JavaScript program to Join all given URL segments together, then normalizes the resulting URL

0

Write a JavaScript program to Join all given URL segments together, then normalizes the resulting URL.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

const URL_Join = (...args) =>
  args
    .join('/')
    .replace(/[\/]+/g, '/')
    .replace(/^(.+):\//, '$1://')
    .replace(/^file:/, 'file:/')
    .replace(/\/(\?|&|#[^!])/g, '$1')
    .replace(/\?/g, '&')
    .replace('&', '?');

console.log(URL_Join('http://www.google.com', 'a', '/b/cd', '?foo=123', '?bar=foo'));

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now