URL クラスを使うと、以下のように URL をパーツに分解するのが可能ですが:
> new URL('http://example.com/foo/bar?x=1#hash')
{
hash: "#hash"
host: "example.com"
hostname: "example.com"
href: "http://example.com/foo/bar?x=1#hash"
origin: "http://example.com"
password: ""
pathname: "/foo/bar"
port: ""
protocol: "http:"
search: "?x=1"
username: ""
}
もし、その一部を書き換えて再構築したい場合はこうする:
> var urlObj = new URL('http://example.com/foo/bar?x=1#hash')
> urlObj.hostname = 'qiita.com'
> urlObj.pathname = '/unk'
> urlObj.hash = 'cnk'
> urlObj.toString()
"http://qiita.com/unk?x=1#cnk"