在
在 regex101.com 上查看其他演示。
有人可以帮我处理正则表达式吗?
-
它应该包括除
foo.[contenthash].js
和bar.[contenthash].js
之外的所有 JavaScript 文件。 -
它只应跳过文件名开头出现
foo
或bar
的文件。 -
它不应该跳过部分文件名,即 不应跳过
fo.[contenthash].js
(请参阅foo
中缺少的o
(。
有人可以帮忙吗?提前谢谢。
您可以在开头使用带有锚点的负面展望:
^(?!foo|bar).*.js$
在
JavaScript
中,这可能是:
let files = ['test.12345.js', 'foo.12345.js', 'bar.12345.js'];
let regex = /^(?!foo|bar).*.js$/
files.forEach(function(file) {
console.log(regex.test(file));
});
在 regex101.com 上查看其他演示。
import re
files = ["index.js", "index.html", "main.js", "foo.js", "bar.js", "foo.something.js", "foo.something1.js", "bar.something.js", "bar.something1.js", "fo.something.js"]
patternjs = r"^.+.js$"
patternfoo = r"^fo.+.js$"
patternbar = r"^ba.+.js$"
patternexcludefoo = r"^foo.w+.js$"
patternexcludebar = r"^bar.w+.js$"
for file in files:
resultfoo = re.match(patternfoo, file)
resultbar = re.match(patternbar, file)
resultjs = re.match(patternjs, file)
resultexcludefoo = re.match(patternexcludefoo, file)
resultexcludebar = re.match(patternexcludebar, file)
if resultfoo or resultbar:
print(file + " is js file either of foo type or bar type")
if resultjs:
print(file + " is js file")
if resultexcludefoo or resultexcludebar:
print(file + " is to be excluded")
获得字符串后,您可以相应地操作文件
PS:代码可能有些达不到标准,因为我在 2 天前才学习了正则表达式。这就是我想到的代码。没有检查JavaScript,但这些模式在JavaScript中也可以工作(它不支持回溯,我没有在此代码中使用它(