Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 652
David C Avatar asked Feb 11 '17 13:02

David C


2 Answers

There you go:

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

See it working on regex101.com.


As mentioned by others, the regex engine used by JavaScript does not support all features. For example, negative lookbehinds are not supported.
like image 185
Jan Avatar answered Nov 14 '22 22:11

Jan


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"));
like image 25
Sagar V Avatar answered Nov 14 '22 21:11

Sagar V