I am trying to write a regex to filter non-minified JavaScript files from an array.
Currently, the following expression works:
array_filter($paths, function($path) {
return preg_match('@\.js$@', $path) && !preg_match('@\.min\.js$@', $path);
});
However, I wanted to write this as a single regular expression:
preg_grep('SOME_REGEX_HERE', $paths);
You can use lookbehind:
preg_match('@(?<!\.min)\.js$@', $path);
Here (?<!\.min)
is negative lookbehind that makes sure .js
is matched only when it is not preceded by .min