Ad

Match Paths That Do Not Have __magic__ Folders

- 1 answer

I have a tree of components and inside of that tree are __magic_names__ folders (e. g. __tests__, __fixtures__, etc.). I want to filter out any files inside of these __magic_names__ folders. I'm using webpack and using require.context to slurp up my components - and I don't want my tests to ship with my production code.

I have the following regular expression, which is supposed to filter out every file path that contains a double-underscore. Unfortunately, it matches paths with __magic__ folders too:

^./((?!=__)[^/]+?(?!=__)/)*((?!=__)[^/]+?(?!=__)).jsx?$

Should work:

./SomeComponent.js
./SomeComponent.jsx
./SomeComponent/SomeComponent.js
./SomeComponent/SomeComponent.jsx
./SomeComponent/ChildComponent/Child.js
./SomeComponent/ChildComponent/Child.jsx

Should fail

./__magic__/SomeComponent.js
./__magic__/SomeComponent.jsx
./SomeComponent/__magic__/SomeComponent.js
./SomeComponent/__magic__/SomeComponent.jsx
./SomeomeComponent.jsx
./SomeComponent/__magic__/SomeComponent.js
./SomeComponent/__magic__/SomeComponent.jsx
./SomeComponent/__magic__/ChildComponent/Child.js
./SomeComponent/__magic__/ChildComponent/Child.jsx
./SomeComponent/ChildComponent/__magic__/Child.js
./SomeComponent/ChildComponent/__magic__/Child.jsx

Debuggex visualizes it this way:

Regular expression visualization

And here's a link to the Debuggex Demo for those who want to play around with it in more detail.

What am I doing wrong?

Ad

Answer

And it turns out that the issue was simply that I was using the wrong syntax for negative look-aheads. (?!=__) is wrong, (?!__) is right.

The corrected regular expression:

^./((?!__)[^/]+?(?!__)/)*((?!__)[^/]+?(?!__)).jsx?$
Ad
source: stackoverflow.com
Ad