---
title: "parseUrl"
description: "将 URL 字符串解析为包含协议、主机、路径、查询参数等字段的结构化对象。"
seo_title: "parseUrl"
seo_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/docs/utilities/url/parse-url"
---
# parseUrl

> 将 URL 字符串解析为包含协议、主机、路径、查询参数等字段的结构化对象。

## 用法

`parseUrl` 函数用于解析 URL 字符串为结构化对象。

```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')
// 解析相对 URL
```

## API

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

解析 URL 字符串为结构化对象。

### 参数

**url** (`string`) *required*: 要解析的 URL 字符串。

**base** (`string`): 可选的基础 URL，用于解析相对路径。

### 返回值

**返回值** (`ParsedUrl | null`): 解析后的 URL 对象，解析失败返回 null。

## Changelog

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


## Sitemap

See the full [sitemap](/sitemap.md) for all pages.
