Use the code PERFMATTERS for 10% off!

How to Disable or Enable Scripts with Regex

You can disable or enable scripts in the Perfmatters Script Manager using Regex (regular expressions). This is especially helpful for sites that have a more complex URL structure in place or dynamically generated pages (archives, search, etc.).

How to Use Regex

If you aren’t familiar with Regex, we recommend utilizing this online tool: regex101.com. Kinsta also has some great examples of how to use Regex.

Disable Scripts Using Regex

Let’s say for example you wanted to disable scripts across your category URLs, but nowhere else. In this case, you could use a regular expression for the category URL. With regular expressions, there are multiple ways you can write them, here are a few variations that would work.

Variation 1

This disables the script whenever the URL contains the word category.

(category)
Disable script with Regex
Disable script with Regex

Variation 2

This disables the script whenever the URL contains /category/.

(\/category\/)

You could target multiple categories like this.

(\/(category1|category2|category3)\/)

Variation 3

This disables the script whenever the URL contains https://domain.com/category/.

(https:\/\/domain.com\/category\/)

Variation 4

The following could be used to target a script that appears on a dynamically generated WordPress search results page.

(\/\?s=)

You could use that either to disable a script by that regex pattern, or you could use it to enable a script by that regex pattern that has already been disabled everywhere.

Enable Scripts Using Regex

You can also use Regex as an exception when disabling the script everywhere.

Enable script with Regex
Enable script with Regex

Perhaps you have a plugin that you want to disable everywhere, but you need it to run on two pages.

(\/(page1|page2)\/)

Was this article helpful?

Related Articles