How to Use OptinMonster’s Matching The Pattern RegEx Rules

Are you wanting to use OptinMonster’s Display Rules to have a campaign target a specific type or URL pattern? Do you want to show a campaign when a user comes from a referrer with certain letters or numbers in the Referral URL? If you answered yes, then this guide is for you.

Note: This guide presumes you have read OptinMonster’s Display Rules Guide. If not, please read that guide first before continuing.
  1. What is Matching the Pattern and RegEx
  2. Helpful Tools
  3. Examples

What is “Matching the Pattern” and RegEx?

As of Fall 2018, there are currently 5 rules that have the “matches the pattern” option which is the use of RegEx. The current 5 rules are the following:

Once you understand how RegEx works for one rule, it can easily be applied to other rules. See specific usage examples for each.

What is RegEx?

RegEx stands for “Regular Expression”.

“A regular expression is a pattern that the regular expression engine attempts to match in input text. A pattern consists of one or more character literals, operators, or constructs.” – Microsoft

In simpler terms, RegEx looks for characters: be it letters, numbers, or specific symbols to identify a pattern. The results permitted or revoked depend on whether the Input to the Regular Expression Engine matches the pattern being employed by the engine.

Because RegEx engines evaluate “patterns of characters”, OptinMonster has employed the phrase, “Matches the Pattern” because it is a more easily recognizable phrase to describe what is occurring.

An example of a standard RegEx pattern is:

^(?!es/).+

The above will match any URL path that does not start with es/, such as mysite.com/es/


Helpful Tools

We have found the following sites beneficial for using and testing out RegEx.

If you’re trying to set up your own “Match the Pattern” RegEx formula, first test them out on the sites above, before testing in OptinMonster. It should help speed up the testing process and ensure the quickest Rule Creation.

Things to note in the examples below:

The use of the b signifies a word boundary. It is used in this example:

^(?!esb/?).+

In plain english: the b signifies the change of words or segments between words. For instance, you may desire to block es for Spanish pages but not a page with the URL path of espresso. The b word boundary matches the string that does not start with the word ‘es’ and may or may not end with /.

Meaning it will not pass the rule (it won’t show a campaign from OptinMonster):

  • mysite.com/es
  • mysite.com/es/
  • mysite.com/es/adios

But will show and campaign on this page:

  • mysite.com/espresso

Examples

Exclude a Specific Language

You want to use the current URL path rule to exclude Spanish pages where es/ is the language differentiator.

The RegEx Pattern (“Matching the Pattern”) is:

^(?!esb/?).+

You will notice the code has the b for the Word Boundary.

When the above is added, it will match (i.e.: show the campaign) on any URL path that does not start with es/.

This means it will show the campaign here: mysite.com, but not here: mysite.com/es.

Only a Specific Language

You want to use the current URL path rule to target ONLY Spanish pages where es/ is the language differentiator.

The RegEx Pattern (“Matching the Pattern”) is:

^(?=esb/?).+

When the above is added, it will match (i.e.: show the campaign) on any URL path that does start with es/.

This means it will show the campaign here: mysite.com/es, but not here: mysite.com.

Exclude campaign from Showing on Multiple Language Directories

Let’s say your default language is English for your site, so that site shows on mysite.com while your other sites load on URL paths like: mysite.com/es/ or mysite.com/de/ or similar language structures, etc.

In this case, you do not want your campaign to load on those other languages. Therefore, you could enter in a slightly more complex RegEx pattern to prevent the campaign from loading on pages that start with those certain language pre-fixes:

In the situation where you have the following languages: es, de, ja, and ru, you could use the current URL path rule and set the following RegEx Pattern (“Matching the Pattern”):

^(?!(es|de|ja|ru)b\/?).+

The above will prevent a campaign from showing on pages where the url path (the part after the .com) begins with:

  • es/
  • de/
  • ja/
  • ru/

Essentially, the campaign would only show here: mysite.com and related English pages, but never the pages of those other languages.

Visitors from a specific traffic source (Referrer Detection)

In this scenario, you will be using the referrer URL rule and you will be looking at the full URL and not just the URL Path. A URL Path is anything after the .com, such as about-us in the Full URL: http://www.mysite.com/about-us

Therefore, you could have something like this that shows a popup for any user coming from Facebook, Twitter, or Pinterest to your site:

?=(facebook|twitter|pinterest)+.com

and this would prevent the campaign from showing is a web visitor comes from one of those referral URLs:

?!(facebook|twitter|pinterest)+.com
Visitors with a specific URL parameter

URL Parameters are the items you see after a ? symbol in the full URL of a site, such as mysite.com?name=Archie.

URL Parameters do not constitute an independent page, but they can hold variable information used by the pages with which you’re interacting. This can be beneficial when it contains information such as showing information from your Email Service Provider or passing on lead data.

For instance, you could use the URL query arg rule and have something like the following that shows a popup for any user coming from your Email Newsletter when the Parameter contains email-newsletter:

?(email-newsletter)

and this would prevent the campaign from showing if it contains email-newsletter:

?!(email-newsletter)

An example where this can be used is OptinMonster itself. Many cookies set by OptinMonster start with the characters: om. Perhaps your website sets its own cookies for logged in users. If so, perhaps your cookies start with: MySite-Logged-In.

Therefore, you could use the cookie rule and decide you want to show a campaign only to your logged in users by using the following:

?=(MySite-Logged-In)

and below would prevent the campaign from showing if they are logged in and have the cookie MySite-Logged-In:

?!(MySite-Logged-In)
Visitors with a specific URL anchor tag

Anchor Tags are used for linking on a specific page. The Table of Contents set of links at the top of this page, which “jump” you to the correct section when you click on them are an example of anchor tags. They show up with # in the URL path, such as mysite.com/about-us#early-history where early-history is the anchor tag.

In this scenario, perhaps you only want to show a campaign if someone was “deeplinked” to your page to that specific anchor tag.

Therefore, you could use the current URL hash rule and decide you want to show a campaign only to those with the anchor tag early-history:

?=(early-history)

and the following RegEx would prevent the campaign from showing if they have early-history as an anchor tag in their URL:

?!(early-history)

FAQs

Q: Is there a simpler way to target my campaigns?

A: Some pre-made RegEx Rules are already natively built into OptinMonster’s Display Rules. You can try to use those pre-made rules such as “Contains”, “Does Not Contain”, “Starts With”, “Ends With”, etc., before attempting to use this Advanced Guide.