CyrefJS
CCyrefJS
فا
Back to documentation
URL/Since 1.38.0

parseQuery

Parses a URL query string into a record of string key-value pairs.

parseQuery(query):Record<string, string>

Behavior

The function creates a URLSearchParams instance from the provided query. If the query starts with ?, that first character is removed before parsing.

Parsing

Each entry from URLSearchParams is copied into a plain Record<string, string> object.

Duplicate Keys

If the same key appears multiple times, assigning each entry to the result object causes later values to overwrite earlier values.

URL Encoding

Query parsing and decoding follow the native behavior of URLSearchParams.

Empty Query

An empty query produces an empty object.

Parameters

querystringrequired

The query string to parse. A leading question mark is optional.

Returns

Record<string, string>

Returns an object containing the parsed query parameters as string key-value pairs.

Examples

01

Basic usage

Parse a query string into an object.

TypeScript
import { parseQuery } from "@cyref/js";

parseQuery("page=2&limit=10");
// {
//   page: "2",
//   limit: "10"
// }
02

With a question mark

A leading question mark is automatically removed before parsing.

TypeScript
parseQuery("?page=2&sort=latest");
// {
//   page: "2",
//   sort: "latest"
// }
03

Encoded values

Values are decoded according to URLSearchParams behavior.

TypeScript
parseQuery("search=hello+world");
// {
//   search: "hello world"
// }
04

Repeated keys

When a key appears multiple times, the last value assigned to that key remains in the returned object.

TypeScript
parseQuery("tag=javascript&tag=typescript");
// {
//   tag: "typescript"
// }
Built with@cyref/js·Official documentation