I’m trying to search for code within a WordPress site, specifically for a facebook pixel. I’m searching for strings using a regex and I know what the string starts with, ends with, and what the string should NOT contain. I have tried other solutions on SO but with no luck.
The string should start with:
fbq(‘track’
End with:
);
and NOT contain:
PageView
The expression that I have been playing with to try and do this search is:
^(?=^fbq('track')(?=.*);$)(?=^(?:(?!PageView).)*$).*$/
From this other StackOverflow question:
Combine Regexp?
However, I keep getting back that this is in an invalid format.
3
Answers
You may use:
Or:
Demo.
Breakdown:
^
– Beginning of the string.(?!.*PageView)
– Negative Lookahead (does not contain "PageView" from this point forward).fbq('track
– Match "fbq(‘track", literally (notice how "(" is escabed:(
)..*
– Match zero or more characters (any characters).);
– Match ");" literally.$
– End of string.You can go with the first one!
I already already test it in the regex software what I use to try the "regexes" when I need to. 😉
I’m going to add my litle gain of sand 🙂
Here you have a good source to read the look-around and look-behind (and negative-look-behind, etc): https://www.regular-expressions.info/lookaround.html
*It contains iformation about the use and restrictions on the most used regex flavors (and it implementation in some programming languages).
First of all, if you are not able to locate the FB Pixel, check if you have Google Tag Manager on the site and perhaps it is added via GTM,
If not, then on with the RegEx…
As this is a script in a template file where it can span multiple lines and have spaces before the text etc, a more flexible pattern would be appropriate.
So the main idea is that you don’t use ^ and $ in your pattern.
Example
The pattern above satisfies the requirements you outlined in the OP, where
fbq('track'
– Literally matches fbq(‘track’ as the start of the string(?!.*?PageView)
– Negative lookahead to fail if PageView is found, .*? is used to lazy match 0 or more characters as we would find PageView sooner than later and don’t need to backtrackfbq('track'
<- Cursor here[^)]*
– Matched 0 or more characters until a closing parenthesis is found excluding it);
– Match ); literally.I am guessing you might be using VSCode, PhpStorm or similar so I selected JS as the flavor in the example for for compatibility.
If you are using grep say in Linux or a bash terminal on Windows(Not sure of Mac due to grep param compatibility) running this from the Theme directory should show you the files and matches.