For absolute URL we can parse with new URL(str); for relative URL with base URL we can have new URL(path, base). How do I parse a relative URL without a base URL? For example, folder/file.ext?a=1&b=2#hash should be parsed into
{
pathname: "folder/file.ext",
search: "?a=1&b=2",
hash: "#hash"
}
Third-party library is fine, but I prefer built-in libraries and functions. Prefer cross-platform (browser/Node.js) solutions. No need for IE.
www.example.com/before your string and parse it ...new URL('folder/file.ext?a=1&b=2#hash', 'http://dummy');as a workaround...) since these need to be preserved until the absolute URL is known. @Stöger Manual string transformations are brittle and should be delegated to a library.