Regular ExpressionsAccelerator for WordPress

A regular expression is a powerful language to match text content by various patterns.

For an instance, we have the Amazon Advertising script inside our page that we want to delay loading:

amzn_assoc_placement = "adunit0";
amzn_assoc_enable_interest_ads = "true";
amzn_assoc_tracking_id = "baskingridgeh-20";

So, the pattern for detecting this script by its body may be:

@(?:^|\W)amzn_assoc_placement\s*=\s*[\'"][\w\-]+[\'"]@

Another example – there is separated script:

<script src="/dir/abc.js">

So, we can match it by:

@/abc\.js@ or src:@/abc\.js@ if is needed to limit searching only in src (see below).

And we can test and debug our patterns and look at the syntax.

Special extensions#

Scope prefixes for styles and scripts#

In exclusions in styles and scripts we can limit the scope of matching, as body, ID or source URL by adding related prefixes:

  • src

    Matching with content of src attribute.

  • id

    Matching with content of id attribute.

  • body

    Matching with body content. Body is taked as from inline script (or style) as from by URL.

This prefixes can be combined, e.g. src,body or id,src, etc. E.g. src,body:@abc@ will search matching src attribute and body with string abc.

Also, if no prefixes are defined then it means matching in all places. E.g. id,src,body:@abc@ equals to @abc@.

Conditions#

Also, results of standard rerular expression matching can be compared by appending with = or != and exact value (with logical ‘&’ operator as well). E.g. @Ab\w\wef$@ = Abcdef & @\Fed(\w)b(\w\)$@ != ca. Also, you can check the result for the empty value by prepending expression with !, for example, !@Ab\w\wef$@.

Leave a Reply