javascript - 匹配没有 __magic__ 文件夹的路径

标签 javascript regex reactjs webpack

我有一个组件树,该树的内部是 __magic_names__文件夹(例如 __tests____fixtures__ 等)。我想过滤掉这些 __magic_names__ 中的任何文件文件夹。我正在使用 webpack 并使用 require.context 吞噬我的组件 - 而且我不希望我的测试与我的生产代码一起发布。

我有以下正则表达式,应该过滤掉每个包含双下划线的文件路径。不幸的是,它与 __magic__ 匹配路径文件夹也是:

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

应该有效:

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

应该失败

./__magic__/SomeComponent.js
./__magic__/SomeComponent.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以这种方式可视化它:

Regular expression visualization

这是指向 Debuggex Demo 的链接对于那些想要更详细地尝试它的人。

我做错了什么?

最佳答案

您可以像这样匹配所有没有双下划线的内容。

/^((?!__).)*$/gm

关于javascript - 匹配没有 __magic__ 文件夹的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34998446/

相关文章:

javascript - 为什么我的 javascript 中有 $ ?

javascript - 重构以使函数可通过 setState 重用

reactjs - React 的 useMemo 方法导致无效的 hook 调用

javascript - 如何防止最后一个刻度渲染?使用 'recharts' 库

javascript - 使用 Form 元素转换/更改 Div 元素

javascript - 简单的 NodeJS/Express 单元测试

正则表达式匹配多个点

c# - 解析 .NET 中类似内容的简单正则表达式问题?

java - 由 : java. util.regex.PatternSyntaxException 引起:非法重复

reactjs - react : Why are props not frozen?