Search code examples
regexperl

Regex for any string not ending on .js


This has been driving me nuts. I'm trying to match everything that doesn't end in .js. I'm using perl, so ?<! etc. is more than welcome.

What I'm trying to do:

Do match these

mainfile
jquery.1.1.11
my.module

Do NOT match these

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

This should be an insanely simple task, but I'm just stuck. I looked in the docs for both regex, sed, perl and was even fiddling around for half an hour on regexr. Intuitively, this example (/^.*?(?!\.js)$/) should do it. I guess I just stared myself blind.

Thanks in advance.


Solution

  • You can use this regex to make sure your match doesn't end with .js:

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

    RegEx Demo

    (?!.+\.js$) is a negative lookahead condition to fail the match if line has .js at the end.