[英]Regex to exclude a pattern in filename
我目前的模式是:
/\.(scss|js|jsx)$/
我想要排除以“-test.js”结尾的文件
我试过了:
/(?!-test)\.(scss|js|jsx)$/
但它不起作用。
由于JS不支持lookbehind,你可以做^(?!.*-test\\.js$).*\\.(js|scss|jsx)$
作为解决方法。
var testFiles = ['p-test.js', 'q-test.scss', 'r-test.jsx', 'p.js', 'q.scss', 'r.jsx', 'sadadf-testddd.js' ]; var myRegexp = /^(?!.*-test\\.js$).*\\.(js|scss|jsx)$/g; testFiles.forEach(function(file) { var match = file.match(myRegexp) console.log(file + " : " + (match ? 'matches' : 'does not match')); })
js|jsx
可以通过使用: jsx?
来改进/加速jsx?
。
scss
可以通过使用scs{2}
来加速。
在较长的“替代品”之前加上较短的“替代品”将提高速度 (jsx?|scs{2})
模式:
/^(.(?!-test\.js$))+\.(jsx?|scs{2})$/
或者将近一半的步骤,由于删除了捕获组:
/^(?!.*-test\.js$).*\.(jsx?|scs{2})$/
测试用例:
a-test.js #fail
b-test.scss #pass
c-test.jsx #pass
d.js #pass
e.scss #pass
f.jsx #pass
g-testg.js #pass
h-testh.scss #pass
i-testi.jsx #pass
试试这个:
/^([a-z0-9-_](?!-test\.))+\.(scss|js|jsx)$/
文件名的所有字符都不应该跟在-test.
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.