RegEx 包含除 foo.js 和 bar.js 之外的所有 JavaScript 文件



有人可以帮我处理正则表达式吗?

  1. 它应该包括除 foo.[contenthash].jsbar.[contenthash].js 之外的所有 JavaScript 文件。

  2. 它只应跳过文件名开头出现foobar的文件。

  3. 不应该跳过部分文件名,即 不应跳过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中也可以工作(它不支持回溯,我没有在此代码中使用它(

最新更新