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
| Token | Description |
|---|---|
| h | Matches the literal character 'h' |
| t | Matches the literal character 't' |
| t | Matches the literal character 't' |
| p | Matches the literal character 'p' |
| s | Matches 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
| Input | Expected |
|---|---|
| https://example.com/search?q=regex | Match |
| http://api.site.com/v1/users?page=2&limit=10 | Match |
| https://example.com/about | No Match |
| https://shop.co/items?id=42#reviews | Match |
| example.com?q=test | No Match |
Try It — Interactive Tester
Match Highlighting(3 matches)
Matches & Capture Groups
72 charsFlags: giMatches: 3Ctrl+Shift+C to copy regex
Related Regex Patterns
Regex to Match URLs
/https?:\/\/[\w.-]+(?:\.[a-zA-Z]{2,})(?:\/[\w./?#&=%-]*)*/gi
Regex to Extract Domain Names
/(?:https?:\/\/)?(?:www\.)?([a-zA-Z0-9-]+(?:\.[a-zA-Z]{2,})+)/gi
Regex to Match Mailto Links
/mailto:[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(?:\?[\w=&%.+-]*)?/gi
Regex to Match Hashtags
/#[a-zA-Z]\w{0,138}/g