【问题标题】:watch files in folder changed using regEx使用正则表达式更改文件夹中的监视文件
【发布时间】:2021-05-19 18:02:12
【问题描述】:

我想看,文件夹的变化:

  • test/index.js
  • te3st/index.js

但不会改变

  • te2st/index.js 不是任何 teNUMBERst/index.js

我使用 chokidar 并从这里获得灵感:chokidar

ignored: ignored: /^.*\b(?:(?!te2st\/index\.js)\w)+\b$/ 可以使用正则表达式,但我的正则表达式没有像预期的那样工作 (regex how to exclude specific characters or string anywhere)。

这个正则表达式在这里工作: https://regex101.com/r/XnKYcf/1/

var chokidar = require('chokidar');
var watcher = chokidar.watch('/home/replays/0.0.24/t*t/index.js',
{ignored: /^.*\b(?:(?!te2st\/index\.js)\w)+\b$/, persistent: true});
watcher
  .on('add', function(path) {console.log('File', path, 'has been added  ');})
  .on('change', function(path) {console.log('File', path, 'has been changed  ');})
  .on('unlink', function(path) {console.log('File', path, 'has been removed  ');})
  .on('error', function(error) {console.error('Error happened', error);})

这是广告否定定义,在这里还不能正常工作。通过正则表达式允许会更实用且更易于阅读。这可能吗?

【问题讨论】:

  • 暂时的解决方法:crontab -e */2 * * * * php ~/doIt.php 读取由正则表达式过滤的完整文件夹

标签: javascript chokidar


【解决方案1】:

这个答案真的没有使用忽略属性。它首先允许几乎任何东西,然后再过滤。也许它更容易理解为具有忽略属性的解决方案,我无法用它来实现它。

var chokidar = require('chokidar');

var watcher = chokidar.watch('/home/x/0.24/t*t/index.js',{ignored: /$^/, persistent: true}); // /$^/ is match nothing

watcher
  .on('add', function(path) {
      if(path.match(/te(|3)st\/index\.js/)){
        console.log('File', path, 'te(||3)st found :) ');
      }
      
})

$ node /home/x/0.0.24/file-watcher.js的输出:

File /home/x/0.0.24/te3st/index.js te(||3)st found :) 
File /home/x/test/index.js te(||3)st found :) 

【讨论】:

    猜你喜欢
    • 2015-04-26
    • 1970-01-01
    • 1970-01-01
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多