Web
URL Query Parameter Regex
Query strings carry parameters after the question mark in a URL, separated by ampersands, such as ?page=2&lang=en. This pattern finds a single key and value pair that starts after ? or &, capturing the key in group 1 and the raw value in group 2. With the g flag it walks through every parameter, which is handy in log analysis, analytics debugging, redirect checks and quick scripts that inspect tracking links. The honest limit is that values are returned still percent-encoded, and keys with unusual characters such as brackets are not matched.
Pattern (JavaScript)
/[?&]([A-Za-z0-9_.-]+)=([^&#\s]*)/Matches one of the characters ?&, then a group (one of the characters A-Za-z0-9_.-, one or more times), then the character "=", then a group (any character except &#\s, zero or more times).
Examples
Matches
- ✓ https://example.com/?page=2
- ✓ /search?q=regex&lang=en
- ✓ ?id=
Does not match
- ✗ https://example.com/page
- ✗ a=b
- ✗ ?=value
Try it
4 matches (JavaScript engine)
https://example.com/?page=2 /search?q=regex&lang=en ?id= https://example.com/page a=b ?=value
Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /[?&]([A-Za-z0-9_.-]+)=([^&#\s]*)/g;
const text = "https://example.com/?page=2";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'[?&]([A-Za-z0-9_.-]+)=([^&#\s]*)')
text = 'https://example.com/?page=2'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/[?&]([A-Za-z0-9_.-]+)=([^&#\s]*)/';
$text = 'https://example.com/?page=2';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("[?&]([A-Za-z0-9_.-]+)=([^&#\\s]*)");
String text = "https://example.com/?page=2";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`[?&]([A-Za-z0-9_.-]+)=([^&#\s]*)`)
text := "https://example.com/?page=2"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"[?&]([A-Za-z0-9_.-]+)=([^&#\s]*)");
var text = "https://example.com/?page=2";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"[?&]([A-Za-z0-9_.-]+)=([^&#\s]*)").unwrap();
let text = "https://example.com/?page=2";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /[?&]([A-Za-z0-9_.-]+)=([^&#\s]*)/
text = 'https://example.com/?page=2'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- How do I decode the captured values?
- Pass each value to decodeURIComponent after replacing + with a space. In other languages, use the standard URL decoding function from the library.
- Why use a regex instead of URLSearchParams?
- URLSearchParams is better for complete URLs in JavaScript. The regex is useful when URLs are embedded in logs or text, or when you are working in a tool that only offers regex search.
- Will it match parameters in the fragment part?
- Yes, if the fragment contains ? or & followed by key=value, because the pattern does not know where the fragment starts. Cut the string at # first if that matters.