【问题标题】:Filtering files in directory with regex.match using Puppeteer使用 Puppeteer 使用 regex.match 过滤目录中的文件
【发布时间】:2021-09-02 04:29:12
【问题描述】:

当我在在线检查器https://regex101.com 中单独测试它们时,我遇到了 regex.match 与匹配的文件名不匹配的问题@

谁能在下面的代码中发现问题?

问:我应该使用 regex.test 代替 match 吗?如果是,当它包含变量时如何创建正则表达式?

它应该匹配以以下开头的所有文件:ES_(Stay) True - Lars Eriksson

文件和目录列表在 fs.readdirSync 找到的路径中:

.DS_Store
ES_(Stay) True - Lars Eriksson (22).mp3
ES_(Stay) True - Lars Eriksson (22).mp3.crdownload
ES_(Stay) True - Lars Eriksson.mp3
ES_(Stay) True - Lars Eriksson.mp3.crdownload
Other - File (22).mp3
Other - File (22).mp3.crdownload
Other - File.crdownload
Other - File.mp3
originals

正则表达式转换为:

/^(ES_\(Stay\) True - Lars Eriksson(?: \([0-9]+\))?.mp3(?:.crdownload?)?)$/

木偶脚本:

const puppeteer = require('puppeteer');
const fs = require('fs');

(async () => {

function escapeRegex(string) {
    return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
}

let path = '/path/to/files/';
let title = 'ES_(Stay) True';
let artist = 'Lars Eriksson';

title = escapeRegex(title);
artist = escapeRegex(artist);

let regex = `/^(${title} - ${artist}(?: \\([0-9]+\\))?\.mp3(?:\.crdownload?)?)$/`;
console.log(regex);

fs.readdirSync(path)
    .filter(f => {
        regex.match();
    })
    .map(f => {
        console.log(f);
    });

})();

【问题讨论】:

    标签: javascript node.js regex puppeteer


    【解决方案1】:

    我认为要将字符串转换为正则表达式,您应该使用 RegExp() 而不仅仅是将其用作字符串

    let regex = new RegExp(`^(${title} - ${artist}(?: \\([0-9]+\\))?\.mp3(?:\.crdownload?)?)$`, 'gi');
    console.log(regex);
    
    • 你也被使用regex.match()当没有什么可以匹配时你期望匹配什么你试图匹配正则表达式而不应该是什么
    f.match(regex)
    

    你的代码应该是这样的

    const puppeteer = require('puppeteer');
    const fs = require('fs');
    
    (async () => {
    
    function escapeRegex(string) {
        return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
    }
    
    let path = '/path/to/files/';
    let title = 'ES_(Stay) True';
    let artist = 'Lars Eriksson';
    
    title = escapeRegex(title);
    artist = escapeRegex(artist);
    
    let regex = new RegExp(`^(${title} - ${artist}(?: \\([0-9]+\\))?\.mp3(?:\.crdownload?)?)$`, 'gi');
    console.log(regex);
    
    let file = fs.readdirSync(path),
    matched = file.filter(f => f.match(regex))
    console.log(matched)
    })();
    

    结果

    0: "ES_(Stay) True - Lars Eriksson (22).mp3"
    1: "ES_(Stay) True - Lars Eriksson (22).mp3.crdownload"
    2: "ES_(Stay) True - Lars Eriksson.mp3"
    3: "ES_(Stay) True - Lars Eriksson.mp3.crdownload"
    

    【讨论】:

    • 这似乎仍然与文件名不匹配。当我在regex101.com 上使用正则表达式时,它可以匹配变化。
    • 我编辑了问题并在其上添加了匹配的数据
    • 而且你的 regex101 是空的)':
    • 你知道一个内置函数来为正则表达式转义字符串,而不是使用自定义的 escapeRegex(string) 函数吗?
    • 我认为 js 中没有内置函数可以执行此操作,您可以搜索库以另一种方式将字符转换为十六进制并使用它们,您可以尝试此功能function toHex(str) { let text = ''; for(let i in str) { text += "\\x"+ str[i].charCodeAt(0).toString(16).toUpperCase(); } return text } -用法如escapeRegex > toHex("str")
    猜你喜欢
    • 1970-01-01
    • 2021-08-07
    • 1970-01-01
    • 1970-01-01
    • 2013-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-20
    相关资源
    最近更新 更多