Search results for "{{ search.query }}"

No results found for "{{search.query}}". 
View All Results

Using Regular Expressions

How-to article

What are Regular Expressions?

A Regular Expression (regex or regexp for short) is a special text string for describing a search pattern. You can think of regular expressions as wildcards on steroids.

It is unlimited what a regular expression can include and/or exclude. Here we will be listing the most popular user-requested examples:

Regex Tester

This regex tester can help you build your own regular expressions. Make sure 'javascript' is selected in the menu on the left.

Targeting multiple pages.

Wildcard examples: (where * can be anything)

1: www.example.com/pages/*

^(http|https):\/\/www.example.com\/pages\/.*$

2: www.example.com/pages/*/article/

^(http|https):\/\/www.example.com\/pages\/[^\/]+\/article\/$

3: www.example.com/user/<user_id>/profile/ (numeric user_id)

^(http|https):\/\/www.example.com\/user\/[0-9]+\/profile\/$

Wildcard excluding a certain pattern:
Target all pages which match the pattern but do not include the word "article" or "post":
www.example.com/pages/*/subpage/

^(http|https):\/\/www.example.com\/pages\/(?!article|post)[^\/]+\/subpage\/$

Excluding certain words or patterns in a URL:

1: Target all pages which do not include "/somepage" and "/someotherpage"

^((?!\/somepage|\/someotherpage).)*$

2: Target pages which include the word "page" and do not include "somepage"

(?=.*page.*)(?!.*otherpage).*

Multiple pages which don't follow any pattern:
Target these three pages:
www.example.com/pages/article-one/details/
www.example.com/pages/some-random-word/details/
www.example.com/pages/another-post/details/

^(http|https):\/\/www.example.com\/pages\/(article-one|some-random-word|another-post)\/details\/$

Pages case insensitive:
Target the following page irrelevant of case: http://www.example.com. For example, http://wWw.ExAmPle.com will also match.

/^(http|https):\/www.example.com$/i

Targeting pages which URL can end with or without a trailing slash.

Depending on how your server is set up, the different pages on your site can be referenced in either one of these cases or both:

  • www.example.com/products
  • www.example.com/products/

The difference is the trailing / at the end. In most cases, this is usually the same page showing the same content, however since this is not necessarily the case, Hotjar doesn't assume it is since it could potentially skew results such as Heatmaps.

This is usually a problem when you set up a Heatmap using the following URL:
www.example.com/products

But you site or external sites are linking to this version of the URL:
www.example.com/products/

How to target both URLs using a Regular Expression

In order to target both the examples shown above, this simple Regular Expression can help you do that:

^(http|https):\/\/www.example.com\/products\/?$

This regular expression will match visits if the URL ends with '/products' OR '/products/'.

If the URL you intend to target might contain a query string or fragment you'll need to use a slightly more complex expression:

^(http|https):\/\/www.example.com\/products\/?([?#]+.+)?$

This regular expression will match visits if the URL ends with '/products' OR '/products/', as well as '/products?name=value' and '/products/#fragment'.

If you are setting up a Heatmap, you will notice Hotjar asks you for a 'Screenshot Page URL'. Simply set the screenshot page to one of the variations, such as "www.example.com/products"

Targeting multiple IPs (used in IP Blocking).

IP Range Regular Expression Builder

We highly recommend using this tool to generate IP ranges in regex format:
http://www.analyticsmarket.com/freetools/ipregex

The examples below are just examples using local IPs. When using IP Blocking you should use public IPs.

Simple IP match (single IP):

192.168.0.1

192.168.0.1 or 192\.168\.0\.1

Both would work since in regex, the "." character means "any character".

IP Ranges

192.168.0.14 - 192.168.0.62

192\.168\.0\.(1[4-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]|6[0-2])

192.168.0.0 - 192.168.0.255

192\.168\.0\.\d{1,3}

CIDR notation IPs:

127.76.111.64/28

127\.76\.111\.(6[4-9]|7[0-9])

Note:

For CIDR notation IPs, you can use this CIDR to regex converter: http://d.xenowire.net/cidr2regex.php

Different IPs

Target all these IPs:
192.168.0.1
192.168.0.100
192.168.0.150

192\.168\.0\.(1|100|150)

Using Regular Expressions

How-to article