How to Use Regular Expressions to Target a Complex Query String in URLs
Use regular expressions to target specific keywords within visitor search queries on your website and extract valuable insights from complex query strings.
Benefits of targeting complex query strings in URLs using regex:
- Gather insights from a specific audience type
-
Combine query strings for a better response
Simple Search URL
To illustrate, consider the example of Flickr, which supports both simple and complex search URLs for advanced searches. For instance:
Simple Search URL example: https://secure.flickr.com/search/?q=kittens
- The search page resides at "/search/"
- Keywords follow the pattern "?q="
- To cater to various keywords, utilize the "or" pipe operator
Regex Configuration for the Example:
This configuration ensures your survey appears whenever visitors search for kittens, puppies, or bunnies.
Complex Search URL
By using complex query string the following URL:
d=taken-20170101-20261231&ct=0&mt=all&adv=1
which offers advanced search functionality, allowing visitors greater control of keywords, image type, date range, and Creative Commons licensing, you can get the following parameters:
-
Keywords included: tree
-
Keywords not included: swing
-
Creative Commons licensing: OK for commercial use
-
Taken between: 1/1/2017 and 12/31/2026
-
Content-Type: Photos/Videos
-
Media Type: Photos & Videos
A lot is going on in these search results. However, parsing back through the options selected makes it easier to pick out search keywords, date parameters, and the other advanced options selected.
Now that you know what the search results path can look like, you can start configuring the target URL with a regular expression.
To target a survey of visitors searching for photos of gardens with Creative Commons commercial licensing, you can use two positive lookaheads to ensure all search parameters are included.
For this, you can add the following characters to regex fields:
Subdomain : secure
TLD : flickr.com
Path : search\/\?(?=q=gardens).*(?=l=commderiv)
This pattern will match all search pages that have the "q=gardens" keyword (and won't show up if people use a -gardens keyword) and the "l=commderiv" Creative Commons commercial license for the URL:
https://secure.flickr.com/search/?q=gardens&l=commderiv&ct=0&mt=all&adv=1
1) The rest of the search URL can be anything - other search keywords, different file types, additional date ranges - but if those two terms are not present, then the survey will not show up.
2) Don't forget to add the ".*" before the second positive lookahead, or it will assume that the search URL contains both terms exactly next to each other ("q=gardensl=commderiv"), which is not how it is formatted.
3) If the "q=gardens" is somewhere in the middle of the string, you will have to add a ".*" before it.
That is all about using the complex query string in the regular expression. If you encounter any issues or have any questions, feel free to contact our support team.