How to get a regular expression to match files ending in ".js" but not ".test.js"?

I use webpack, which accepts regular expressions to feed files to bootloaders. I want to exclude test files from the assembly, and test files end with .test.js. So, I'm looking for a regex that will match index.js, but not index.test.js.

I tried using a negative return statement with

/(?<!\.test)\.js$/

but he says the expression is unacceptable.

SyntaxError: Invalid regular expression: /(?<!\.test)\.js$/: Invalid group

Examples of file names:

index.js          // <-- should match
index.test.js     // <-- should not match
component.js      // <-- should match
component.test.js // <-- should not match
+4
source share
3 answers

var re=/^(?!.*test\.js).*\.js$/;
console.log(re.test("index.test.js"));
console.log(re.test("test.js"));
console.log(re.test("someother.js"));
console.log(re.test("testt.js"));
Run code
+1
source

There you go:

^(?!.*\.test\.js$).*\.js$

, regex101.com.


, , JavaScript, . , lookbehinds .
+4

Javascript does not support negative lookbehinds, but lookarounds:

^((?!\.test\.).)*\.js$

Demo

+2
source

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


All Articles