0

I have the following code:

fs.readdir('.', (err, files) => {
    files.forEach(file => {
        if (file.match(/^\Qcustom_\E.+\d\Q.pid\E/)) {
            var pid = fs.readfile(file, function (err, data) {
                if (err) throw err;
                return data.toString();
            });
            console.log(pid);
        }
    });
});

And in my directory, I have several files such as custom_Component1.pid, custom_Component2.pid, custom_NewComponent3.pid, etc. with each of these containing just a single line (the pid).

However, the code above doesn't match the files, even though according to my Regex101, the regex is correct.

If I console.log(file), the filename shows up exactly how I expect it to, just as custom_Component1.pid

MrDuk
  • 16,578
  • 18
  • 74
  • 133
  • 1
    Did you forget to switch it to JavaScript mode? It does not work for me in JavaScript mode but does in PHP mode. You don't need to mess with \Q \E just use `custom_.+?\d\.pid` – markbernard May 19 '17 at 18:34
  • 1
    Use `/^custom_.+\d\.pid/` Dot must be escaped to match a literal dot. – Wiktor Stribiżew May 19 '17 at 18:37

1 Answers1

1

It doesn't look like Javacript supports \Q or \E, review the following: Javascript equivalent of Perl's \Q ... \E or quotemeta()

Why won't the following regexp work? If you need to be stricter with the matching, describe your exact requirements and I'm sure you will get a quick response of a regexp that works.

/^custom_.+\d\.pid/g

Community
  • 1
  • 1
Lance Whatley
  • 2,395
  • 1
  • 13
  • 16