2017-09-16 129 views
0

在nodejs中,如果給定一個字符串「foo- *」,我如何才能找到以foo開頭的所有目錄名?如何查找與nodejs中的通配符匹配的任何目錄?

E.g.搜索將返回FOO酒吧,FOO奶酪等

我有什麼至今:

// If The directory name contains a wildcard character 
// search all directory names with the preceding characters. 
let wildcard = '*'; 
if (directory.indexOf(wildcard) !== -1) { 
    // Search for directories that contain the directory string (without the *). 
} 

我什麼也看不見的文件系統模塊中是有用的。

回答

0

在低級別上,有fs.readdir()可讀取所有目錄條目。

你可以用它來:

  • 讀取目錄中的要處理
  • 使用fs.stat(),以確定該條目是一個目錄
  • 使用一個模塊像minimatch相匹配的所有條目目錄名稱
  • 可選擇遞歸到目錄中,如果要匹配嵌套目錄以及

爲了讓您的生活更輕鬆一點,您可以使用glob模塊,它可以完成上述所有功能。

+0

我去了glob模塊。 – chap

相關問題