Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match ".js" and ".css" but exclude ".min.js" and ".min.css"?

Tags:

regex

I tried the following without success:

\.(?!min)(js|css)$

Regex 101

I'm not very familiar with negative lookaheads, so I'm probably doing something wrong.

How can my regex be modified to match .js and .css but exclude .min.js and .min.css?

like image 886
Nate Avatar asked Sep 07 '25 07:09

Nate


2 Answers

You've got it quite right, except

  • you need to place it before the dot
  • you need to use lookbehind instead of lookahead
(?<!\.min)\.(js|css)$

With lookahead this is more complicated, altough you might manage it if you matched the complete filename:

^(.{0,3}|.*(?!\.min).{4})\.(js|css)$

(a string shorter than 4 characters or one whose last 4 characters are not .min, isn't this horrible?)

like image 68
Bergi Avatar answered Sep 10 '25 10:09

Bergi


You need to use negative lookbehind:

(?<!\.min)\.(js|css)$

RegEx Demo

like image 34
anubhava Avatar answered Sep 10 '25 08:09

anubhava