[英]file.match(regex) not matching filename provided
我有以下代碼:
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);
}
});
});
在我的目錄中,我有幾個文件,例如custom_Component1.pid
, custom_Component2.pid
, custom_NewComponent3.pid
等,每個文件僅包含一行(pid)。
但是,即使根據我的Regex101 ,上面的代碼也不匹配文件,正則表達式是正確的。
如果我console.log(file)
,文件名顯示的正是我期望的樣子,就像custom_Component1.pid
它似乎不像Javacript支持\\Q
或\\E
,請查看以下內容: 與Perl的\\ Q ... \\ E或quotemeta()等效的Javascript
為什么以下正則表達式不起作用? 如果您需要嚴格匹配,請描述您的確切要求,我相信您會很快獲得有效的正則表達式的響應。
/^custom_.+\\d\\.pid/g
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.