A query parameter is a value that is appended to the end of a URL, preceded by a “?” character. It is used to pass data to the server in the form of key-value pairs. For example, consider the following URL:
https://www.example.com/search?q=query+parameters&page=2
In this URL, there are two query parameters: “q” and “page”. The “q” parameter is set to “query+parameters”, and the “page” parameter is set to “2”.
Query parameters are often used in web applications to specify the specific data that the server should return, or to specify the criteria that should be used to filter the data. They are passed to the server as part of the HTTP request, and can be accessed by the server-side script or application that processes the request.
In addition to being used in HTTP requests, query parameters can also be used in other contexts, such as in the URLs of REST API endpoints or in the configuration of software applications.
In this article let’s understand how we can create a regex for URL Query Params and how regex can be matched for URL Query Params.
Regex (short for regular expression) is a powerful tool used for searching and manipulating text. It is composed of a sequence of characters that define a search pattern. Regex can be used to find patterns in large amounts of text, validate user input, and manipulate strings. It is widely used in programming languages, text editors, and command line tools.
Structure of a Query Param
The query param string should have the following criteria and structure-
- It can start with any valid domain, it can contain
www.
orhttp://
orhttps://
etc., - then it must be followed by domain name
- then it will be followed by top level domain(TLD) like .com, .net, .io etc.,
- then it should have query params in the url –
?
followed bykey=value
pairs of query params separated by&
Regex for checking if Query Param is valid or not
Regular Expression-
/^([^?=&]+)(=([^&]*))/igm
Test string examples for the above regex-
Input String | Match Output |
---|---|
.as10 | does not match |
https://www.google.com?q=debugpointer | matches |
#@$some .qwq.eras | does not match |
www.google.com?something=abc&you=123 | matches |
debugpointer.com | does not matches |
Here is a detailed explanation of the above regex-
/^([^?=&]+)(=([^&]*))/igm
1st Capturing Group ([^?=&]+)
Match a single character not present in the list below [^?=&]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
?=& matches a single character in the list ?=& (case insensitive)
2nd Capturing Group (=([^&]*))
= matches the character = with index 6110 (3D16 or 758) literally (case insensitive)
3rd Capturing Group ([^&]*)
Match a single character not present in the list below [^&]
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
& matches the character & with index 3810 (2616 or 468) literally (case insensitive)
Global pattern flags
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
Hope this article was useful to check if the string is a valid URL Query Param or not. In conclusion, query parameters play a crucial role in web applications by enabling the transmission of key-value data between clients and servers. They are integral to specifying data, filtering results, and configuring APIs. Understanding how to create and validate regex patterns for query parameters enhances the ability to handle and manipulate URLs effectively. Regular expressions, as a powerful tool, facilitate the identification of patterns in text, contributing to better data handling and validation in various programming and application contexts.