我正在忙于一个节点/express应用程序,我试图忽略所有包含.part或.temp的文件名作为文件名的一部分。我写的正则表达式完全失败了...任何想知道我在做什么错?
我的言论:
/[xyz]*.part+|.temp+/
如果将表达式插入Regex101.com或类似,则应该看到问题。
如果要滤除包含.part
或.temp
的文件名:
if (!/.(?:part|temp)/.test(filename)) {
// Doesn't have it, the name's okay
}
,如果您愿意,它可能是/.part|.temp/
。(这更容易理解...)
测试的示例:
var tests = [
{str: "foo", expect: true},
{str: "foo.part", expect: false},
{str: ".partfoo", expect: false},
{str: "foo.partfoo", expect: false},
{str: "bar", expect: true},
{str: "bar.temp", expect: false},
{str: ".tempbar", expect: false},
{str: "bar.tempbar", expect: false}
];
tests.forEach(function(entry) {
var result = !/.(?:part|temp)/.test(entry.str);
if (result == entry.expect) {
console.log("'" + entry.str + "' => " + result + " GOOD");
} else {
console.log("'" + entry.str + "' => " + result + " ERROR should be " + entry.expect);
}
});
.as-console-wrapper {
max-height: 100% !important;
}