javascriptregexwebpack

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


I am using webpack which takes regular expressions to feed files into loaders. I want to exclude test files from build, and the test files end with .test.js. So, I am looking for a regular expression that would match index.js but not index.test.js.

I tried to use a negative lookback assertion with

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

but it says that the expression is invalid.

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

example files names:

index.js          // <-- should match
index.test.js     // <-- should not match
component.js      // <-- should match
component.test.js // <-- should not match

Solution

  • 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"));