Reputation: 2038
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
Upvotes: 7
Views: 7207
Reputation: 43169
There you go:
^(?!.*\.test\.js$).*\.js$
See it working on regex101.com.
Upvotes: 7
Reputation: 12478
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"));
Upvotes: 2