Javascript 如何获取正则表达式以匹配以“quot;结尾的文件?”;。js";但并非如此;。test.js“;?
我正在使用webpack,它使用正则表达式将文件馈送到加载程序。我想从构建中排除测试文件,测试文件以Javascript 如何获取正则表达式以匹配以“quot;结尾的文件?”;。js";但并非如此;。test.js“;?,javascript,regex,webpack,Javascript,Regex,Webpack,我正在使用webpack,它使用正则表达式将文件馈送到加载程序。我想从构建中排除测试文件,测试文件以.test.js结尾。因此,我正在寻找一个正则表达式,它将匹配index.js,但不匹配index.test.js 我试着用否定的回溯断言 /(?<!\.test)\.js$/ /(? 但是它说这个表达式是无效的 SyntaxError: Invalid regular expression: /(?<!\.test)\.js$/: Invalid group 语法错误:无效的正则
.test.js
结尾。因此,我正在寻找一个正则表达式,它将匹配index.js
,但不匹配index.test.js
我试着用否定的回溯断言
/(?<!\.test)\.js$/
/(?
但是它说这个表达式是无效的
SyntaxError: Invalid regular expression: /(?<!\.test)\.js$/: Invalid group
语法错误:无效的正则表达式:/(?)?
示例文件名:
index.js // <-- should match
index.test.js // <-- should not match
component.js // <-- should match
component.test.js // <-- should not match
index.js/Javascript不支持负lookbehind,但支持lookarounds:
^((?!\.test\.).)*\.js$
好了:
^(?!.*\.test\.js$).*\.js$
看到了。
正如其他人所提到的,JavaScript使用的正则表达式引擎不支持所有功能。例如,不支持负面外观滞后。
var re=/^(?。*test\.js.*\.js$/;
log(re.test(“index.test.js”);
log(re.test(“test.js”);
log(re.test(“someother.js”);
console.log(re.test(“testt.js”);
很长一段时间没有查看webpack,但我似乎记得您可以跳过正则表达式并传递一个函数。在您的例子中类似于这样:function(path){return path.endsWith('.js')&!path.endsWith('test.js')}
不过,我可能会把它与其他捆绑包混淆在一起。我不想再次打断你的回答,这实际上是最好的正则表达式(即,只有一个在前瞻中具有行尾位置的正则表达式,无法捕获中间字符串test.js)