Regex to Match URLs with Query Parameters

Match URLs that contain query parameters with this regex pattern. Validates URLs with query strings and optional hash fragments. Free tool.

Regular Expression

/https?:\/\/[\w.-]+\.[a-zA-Z]{2,}(?:\/[\w./-]*)*\?[\w=&%.+-]+(?:#[\w-]*)?/gi

Token Breakdown

TokenDescription
hMatches the literal character 'h'
tMatches the literal character 't'
tMatches the literal character 't'
pMatches the literal character 'p'
sMatches the literal character 's'
?Makes the preceding element optional (zero or one times)
:Matches the literal character ':'
\/Matches a literal forward slash
\/Matches a literal forward slash
[\w.-]Character class — matches any one of: \w.-
+Matches the preceding element one or more times (greedy)
\.Matches a literal dot
[a-zA-Z]Character class — matches any one of: a-zA-Z
{2,}Matches 2 or more times
(?:Start of non-capturing group
\/Matches a literal forward slash
[\w./-]Character class — matches any one of: \w./-
*Matches the preceding element zero or more times (greedy)
)End of group
*Matches the preceding element zero or more times (greedy)
\?Matches a literal question mark
[\w=&%.+-]Character class — matches any one of: \w=&%.+-
+Matches the preceding element one or more times (greedy)
(?:Start of non-capturing group
#Matches the literal character '#'
[\w-]Character class — matches any one of: \w-
*Matches the preceding element zero or more times (greedy)
)End of group
?Makes the preceding element optional (zero or one times)

Detailed Explanation

This regex specifically matches URLs that contain query parameters. Here is the token-by-token breakdown:

https? — Matches 'http' or 'https' as the protocol. The s is optional due to the ? quantifier.

:// — Matches the literal '://' separator between the protocol and domain.

[\w.-]+ — Matches the domain name including subdomains. Word characters, dots, and hyphens are allowed.

.[a-zA-Z]{2,} — Matches a dot followed by the top-level domain of at least two letters.

(?:/[\w./-]) — An optional non-capturing group repeated zero or more times for the URL path. Each segment starts with a slash and allows word characters, dots, slashes, and hyphens.

? — Matches a literal question mark that begins the query string. This is required (not optional), ensuring the URL must have query parameters to match.

[\w=&%.+-]+ — Matches the query string content. It allows word characters, equals signs (for key=value pairs), ampersands (parameter separators), percent signs (encoded characters), dots, plus signs, and hyphens. At least one character is required.

(?:#[\w-]*)? — An optional non-capturing group that matches a fragment identifier (hash). It starts with a literal # followed by zero or more word characters or hyphens.

The g flag enables global matching and the i flag makes it case-insensitive. This pattern is useful for identifying URLs that pass data via query strings, such as search result links or API endpoints.

Example Test Strings

InputExpected
https://example.com/search?q=regexMatch
http://api.site.com/v1/users?page=2&limit=10Match
https://example.com/aboutNo Match
https://shop.co/items?id=42#reviewsMatch
example.com?q=testNo Match

Try It — Interactive Tester

//gi
gimsuy

Match Highlighting(3 matches)

https://example.com/search?q=regex http://api.site.com/v1/users?page=2&limit=10 https://example.com/about https://shop.co/items?id=42#reviews example.com?q=test

Matches & Capture Groups

#1https://example.com/search?q=regexindex 0
#2http://api.site.com/v1/users?page=2&limit=10index 35
#3https://shop.co/items?id=42#reviewsindex 106
Pattern: 72 charsFlags: giMatches: 3

Ctrl+Shift+C to copy regex

Customize this pattern →