How to Use OptinMonster’s Matching The Pattern RegEx Rules

Are you wanting to use OptinMonster’s Display Rules to have an optin target a specific type or URL pattern? Do you want to show an optin 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 for Page-Targeting. If not, please read that guide first before continuing.
  1. What is Matching the Pattern and RegEx
  2. Helpful Tools
  3. Examples
  4. FAQs
RegEx Not Working? It could be that you are missing the Extra Backslash Required for OptinMonster

What is “Matching the Pattern” and RegEx?

As of Spring 2017, there are currently 5 Rules that have an option to use the “Match The Pattern”, which is the use of RegEx. The current 5 rules are the following:

  • Visitors browsing specific pages
  • Visitors from a specific traffic source (Referrer Detection)
  • Visitors with a specific URL parameter
  • Visitors with a specific cookie
  • Visitors with a specific URL anchor tag

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/

Whereas something usable for OptinMonster, utilizing the double slashes for escaped characters, and allowing ending slash is:

^(?!es)\\b\\/?).+

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.

IMPORTANT – An Extra Backslash is Required for OptinMonster: Due to OptinMonster’s use of RegEx and other ways we escape and clean the input, any backward slash \ will actually need to be doubled to: \\ So something like the following should work:

^(?!es)\\b\\/?).+

To learn more about “escape characters”, check out this guide. You can see examples of the extra backslash being added in our FAQ here.

Things to note in the examples below:
The use of the \b signifies a word boundary. It is used in this example:

^(?!es)\\b\\/?).+

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 an Optin from OptinMonster):

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

But will show and optin on this page:

  • mysite.com/espresso

Examples

How to Use RegEx for a Multi-Lingual Site

These examples focus primarily on the Rule: Visitors Browsing These Pages, as most multi-lingual site have language files as a sub-folder on their site, such as mysite.com/es or mysite.com/ro.

Visitors-Browsing-These-Pages-RegEx-Rule-for-OptinMonster

EXAMPLE #1: Exclude a Specific Language
You want to exclude Spanish pages where es/ is the language differentiator. Such as showing on optin only on your English pages, that don’t have a language folder.

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

^(?!es)\\b\\/?).+

You will notice the code above uses the double slashes needed for OptinMonster, and also has the \b for the Word Boundary.

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

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

EXAMPLE #2: Only a Specific Language
You want to target ONLY Spanish pages where es/ is the language differentiator.

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

^(?es)\\b\\/?).+/

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

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

EXAMPLE #3: Exclude Optin 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 optin to load on those other languages. Therefore, you could enter in a slightly more complex RegEx pattern to prevent the optin 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 following RegEx Pattern (“Matching the Pattern”):

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

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

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

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


Other Examples

In these examples, we’ll cover a single example (and its negation) for every other rule outside of the “Page Slug” rule, mentioned above.

Who-Should-See-the-Campaign-Section-of-OptinMonster-Builder

EXAMPLE #1: Visitors from a specific traffic source (Referrer Detection)
In this scenario, 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 optin from showing is a web visitor comes from one of those referral URLs:

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

EXAMPLE #2: 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 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 optin from showing if it contains email-newsletter:

?!(email-newsletter)

EXAMPLE #3: Visitors with a specific cookie
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 decide you want to show an optin only to your logged in users by using the following:

?(MySite-Logged-In)

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

?!(MySite-Logged-In)

EXAMPLE #4: 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 an optin if someone was “deeplinked” to your page to that specific anchor tag.

Therefore, you could decide you want to show an optin only to those with the anchor tag early-history:

?(early-history)

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

?!(early-history)

FAQs

Q1: 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.


Q2: Why doesn’t my RegEx work as expected?

A: Due to OptinMonster’s use of RegEx and other ways we escape and clean the input, any backward slash \ will actually need to be doubled to: \\ So something like this:

^(?!es)\b\/?).+

would become this:

^(?!es)\\b\\/?).+

To learn more about “escape characters”, check out this guide.