watch files in folder changed using regEx

326 views Asked by At

i want watch, changes in the folders:

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

but not changes in

  • te2st/index.js not any teNUMBERst/index.js

i using chokidar and got inspiration from here: chokidar

with ignored: ignored: /^.*\b(?:(?!te2st\/index\.js)\w)+\b$/ its possible to use regEx, but my regEx did not work like expected ( regex how to exclude specific characters or string anywhere ).

this regex working here : 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);})

that is the ad negativo definition that does not yet work properly here. Allowing via regex would be much more practical and easier to read. is this possible?

1

There are 1 answers

0
SL5net On

this Answer not use the ignore Attribute really. It allows almost anything at first and then filters later. maybe it's easier to read as a solution with the ignore attribute, i haven't been able to implement it with that.

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 :) ');
      }
      
})

output of $ 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 :)