Useful regex examples for targeting
Split targeting rules support matching regex (regular expression) to serve specific treatment, Regex is a powerful tool to filter a wide spectrum of population with one line of text. We recommended that you test your regex on an external tool to verify it works correctly. Here is one suggested site.
Below are a few useful examples.
Target specific app major version
To target users with app version greater than or equal to 4.5 to get the on
treatment, this regex could be used for matching the appVersion
attribute:
(\[5-9\]\\.\[0-9\]|\[4\]\\.\[5-9\]).*
You can use the matches
operator in the targeting rule editor within the Split user interface to match the value of a passed attribute (such as an app version) to a regular expression and specify treatment assignments for any versions that match the expression.
This approach also works when versions are formatted in Major.Minor
and you want a specific treatment for versions below a certain threshold (for example, versions below 2.1.0
).
Example of passing the appVersion
attribute for the JavaScript SDK:
var attributes = {appVersion: "4.567.33"};
Target all population of specific domain of user emails
For this example, to serve the on
treatment for all employees of split.io, we can use the Regex below for email
attribute:
@split.io$
Example of passing the email attribute for the JavaScript SDK:
var attributes = {email: "bilal@split.io"};
Target users on Chrome version 20 and later
This example we are serving the on
treatment to Chrome users only. However, we want to only use Chrome versions 20 and later for compatibility reasons. The attribute passed is userAgent:
Chrome\/[2-9][0-9]\.
Example for the JavaScript SDK:
var attributes = {userAgent: "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36"};
Target English speaking users world wide
An experiment is designed for English speaking users in any country by detecting the default language setting in the browser, the attribute passed is navigatorLanguage en-.
Example for the JavaScript SDK:
var attributes = {navigatorLanguage: navigator.language};
Target specific URL
To target an experiment that applies only to users that land on a specific URL, the extracted URL can be passed as an attribute to the targeting rule:
http:\/\/mysite\.com\/my_url
Example for the JavaScript SDK:
var attributes = {url: window.location.href};