---
title: "parseUrl"
description: "Parse a URL string into a structured object of protocol, host, pathname, query, and hash, returning null when the input is invalid."
canonical_url: "https://core.mhaibaraai.cn/en/docs/utilities/url/parse-url"
---
# parseUrl

> Parse a URL string into a structured object of protocol, host, pathname, query, and hash, returning null when the input is invalid.

## Usage

`parseUrl` parses a URL string into a structured object.

```ts
import { parseUrl } from '@movk/core'

parseUrl('https://example.com:8080/path?query=1#hash')
// {
//   href: 'https://example.com:8080/path?query=1#hash',
//   protocol: 'https:',
//   host: 'example.com:8080',
//   hostname: 'example.com',
//   port: '8080',
//   pathname: '/path',
//   search: '?query=1',
//   hash: '#hash',
//   auth: '',
//   origin: 'https://example.com:8080'
// }

parseUrl('/path', 'https://example.com')
// parse a relative URL against a base
```

## API

### `parseUrl(url, base?)`

Parse a URL string into a structured object.

### Parameters

**url** (`string`) *required*: The URL string to parse.

**base** (`string`): An optional base URL used to resolve relative paths.

### Returns

**returns** (`ParsedUrl | null`): The parsed URL object, or null if parsing fails.

## Changelog

See commit history for [src/utilities/url/parse.ts](https://github.com/mhaibaraai/movk-core/commits/main/src/utilities/url/parse.ts).

---

- [GitHub](https://github.com/mhaibaraai/movk-core/blob/main/src/utilities/url/parse.ts)


## Sitemap

See the full [sitemap](https://core.mhaibaraai.cn/sitemap.md) for all pages.
