Use code PERFMATTERS for an extra 10% off!
  1. Home
  2. Docs
  3. General
  4. How to disable or enable scripts with Regex

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.

Note: When using Regex as a disable, you should only use Regex for that particular script. Not a combination of Regex and “Current URL.”

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 is an exact match, where $ marks the end of the line. Use case example: Your WooCommerce products have /shop/ in the permalink and you only want to disable a script on your shop archive page.

/\/shop\/$/

Variation 5

The following matches custom post types as well as ecommerce pages. Use case example: You have an LMS with multiple custom post types, such as courses, lessons, etc. You want to match any child pages underneath those, as well as the /checkout/ and /cart/ pages.

/\/(customposttype1|customposttype2|checkout|cart)\//

Variation 6

The following targets the home page and whenever the URL contains /customposttype/. Use case example: You have an LMS with multiple custom post types, such as courses, lessons, etc. You want to target your homepage and also match any child pages underneath the custom post type.

/\.com\/?($|customposttype)\/?/

Variation 7

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)\//

If you have any questions about Regex or the proper syntax, feel free to contact us for help.

Was this article helpful?

Related Articles