Regex
regex
December 19, 20223 min read

Regex for HTTPS URL validation

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. or http:// or https:// 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 by key=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 StringMatch Output
.as10does not match
https://www.google.com?q=debugpointermatches
#@$some .qwq.erasdoes not match
www.google.com?something=abc&you=123matches
debugpointer.comdoes 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.

Share this blog
Tagged in :
regex
Like what you read?
Subscribe to our Newsletter
Subscribe to our email newsletter and unlock access to members-only content and exclusive updates.
About the Author
Satvik
Satvik
Entrepreneur
Satvik is a passionate developer turned Entrepreneur. He is fascinated by JavaScript, Operating System, Deep Learning, AR/VR. He has published several research papers and applied for patents in the field as well. Satvik is a speaker in conferences, meetups talking about Artificial Intelligence, JavaScript and related subjects. His goal is to solve complex problems that people face with automation. Related projects can be seen at - [Projects](/projects)
View all articles
Previous Article
December 24, 20222 min read
Next Article
December 28, 20222 min read