Regex to Match URL Query Parameters

Extract URL query string parameters as key-value pairs. Matches parameters after ? or & delimiters in URL query strings. Free online regex tester.

Regular Expression

/[?&]([a-zA-Z0-9_]+)=([^&#]*)/g

Token Breakdown

TokenDescription
[?&]Character class — matches any one of: ?&
(Start of capturing group
[a-zA-Z0-9_]Character class — matches any one of: a-zA-Z0-9_
+Matches the preceding element one or more times (greedy)
)End of group
=Matches the literal character '='
(Start of capturing group
[^&#]Negated character class — matches any character NOT in &#
*Matches the preceding element zero or more times (greedy)
)End of group

Detailed Explanation

This regex extracts individual query parameters from URL query strings. Here is the token-by-token breakdown:

[?&] — A character class matching either a question mark or an ampersand. The question mark starts the query string, while ampersands separate subsequent parameters. By matching either, the pattern works for the first parameter and all following ones.

([a-zA-Z0-9_]+) — Capturing group 1 matches the parameter name (key). It allows one or more letters (upper or lowercase), digits, and underscores. Most URL parameter names follow this character set, though technically percent-encoded characters are also allowed.

= — Matches the literal equals sign that separates the parameter name from its value.

([^&#]*) — Capturing group 2 matches the parameter value. It captures zero or more characters that are not an ampersand or hash symbol. The ampersand would indicate the start of the next parameter, and the hash would indicate the start of the fragment identifier. This allows empty values where the parameter is present but has no value.

The g flag enables global matching to find all query parameters in the URL. For a URL like ?page=1&sort=name&order=asc, this pattern would match three times, capturing page/1, sort/name, and order/asc as key-value pairs.

This pattern is useful for URL parsing, analytics processing, link analysis, and building URL manipulation utilities. It is commonly used in web development for extracting specific parameters from URLs without using the URL API.

Example Test Strings

InputExpected
?page=1Match
&sort=nameMatch
?q=hello+worldMatch
/path/onlyNo Match
?key=value&other=123Match

Try It — Interactive Tester

//g
gimsuy

Match Highlighting(3 matches)

?page=1 &sort=name ?q=hello+world /path/only ?key=value&other=123

Matches & Capture Groups

#1?page=1 index 0
Group 1:page
Group 2:1
#2&sort=name ?q=hello+world /path/only ?key=valueindex 8
Group 1:sort
Group 2:name ?q=hello+world /path/only ?key=value
#3&other=123index 55
Group 1:other
Group 2:123
Pattern: 28 charsFlags: gMatches: 3

Ctrl+Shift+C to copy regex

Customize this pattern →