A Regular Expression, REGEX, is a special text string for describing a search pattern. Within Hotjar, you can use Regular Expressions to target pages with Feedback widgets or Surveys, or to determine when Hotjar should start recording a user's session.
In addition, Regular Expressions can be used to prevent IP addresses from being tracked by Recordings and Heatmaps. The IP block configurations can be found in your Hotjar settings.
Watch out for common mistakes
- If you include the start and end characters (^ and $) then any URL that includes text before or after the pattern will not be matched. Avoid using them. It's very common for URLs to include query strings at the end, such as the UTM parameter that are added to URLs for tracking purposes. An example would be:
- A forward-slash (/) at the end of the URL is generally optional. If your REGEX includes that character at the end, then a visit to the same URL but without the forward-slash wouldn't match. It is better not to include that final forward slash character.
- There is a limit of 750 characters for all of our REGEX targeting rules. If you go over this limit, an error message stating that There was a problem saving session targeting and tracking settings. Please retry later will alert you of this issue.
Need help building your Regular Expressions?
If you're unfamiliar with Regular Expressions and would like to learn more, we highly recommend taking a quick crash course!
Page Targeting with Regular Expressions
For page targeting, when setting up your tool, there will be a Page Targeting option in which Regular Expressions can be set as the targeting option.
- Wildcard examples (where * can be anything)
- Wildcard excluding a certain pattern
- Excluding certain words or patterns in a URL
- Multiple pages which don't follow any pattern
Is your REGEX not working?
This REGEX tester can help you build regular expressions. Make sure 'PCRE2 (PHP >=7.3)' is selected in the menu on the left.
Wildcard examples (where * can be anything)
Wildcard excluding a certain pattern
All pages which match the pattern but do not include the word "article" or "post" www.example.com/pages/*/subpage
Excluding certain words or patterns in a URL
All pages which do not include "/somepage" and "/someotherpage"
Pages which include the word "page" and do not include "somepage":
Multiple pages which don't follow any pattern
Target these three pages:
Blocking IPs with Regular Expressions
Only IPv4 addresses can be entered at this time. We are unable to support IPv6 or dynamic IP addresses.
Simple IP match (single IP)
192.168.0.1 or 192\.168\.0\.1
Both would work since in regex, the "." character means "any character".
Block the IPs 192.168.0.14 - 192.168.0.62
Block the IPs 192.168.0.0 - 192.168.0.255
IP Range Regular Expression Builder
To generate IP ranges in regex format, check out this free tool:
Block all these IPs: