Regex for any line not ending in .js

It drove me crazy. I am trying to match anything that doesn't end in .js . I use perl , so ?<! etc. More than welcome.

What am I trying to do:

Consistent with these

 mainfile jquery.1.1.11 my.module 

Do NOT match these

 mainfile.js jquery.1.1.11.js my.module.js 

It should be an insanely simple task, but I was just stuck. I looked in the docs for regex , sed , perl , and for an hour and a half on regexr. Intuitively, this example ( /^.*?(?!\.js)$/ ) should do this. I guess I just stared at myself.

Thanks in advance.

+5
source share
3 answers

You can use this regex to make sure the match doesn't end in .js :

 ^(.(?!\.js$))+$ 

RegEx Demo

+8
source

The easiest approach when you have only negative matching conditions is to create a positive regular expression and then check that it does not match.

 if ($string !~ /\.js$/) { print "Doesn't end in .js"; } 

This is easier to understand and more effective than a negative appearance.

Workarounds are needed only when you need to mix positive and negative conditions (for example, "I need to match" foo "from a string, but only when it does not follow" bar "). Sometimes it’s easier to use some simple patterns and logic instead of meeting all your requirements with one complex template.

+1
source

This should fit your needs:

^.*(?<![.]js)$

0
source

Source: https://habr.com/ru/post/1204743/


All Articles