节点 glob 中的方括号有问题

Having problem with square brackets in node glob

我正在使用这个包node-glob

我面临的问题是每当我的路径包含方括号时 [] 它没有给我任何文件。

我就是这样做的:

const glob = require('glob')

const path = 'E:/files/Example [Folder] 1'
const files = glob.sync(path + '/**/*', { 
  nobrace: true,
  noext: true
})

括号 () 或大括号 {} 没有问题,但方括号 [].

我正在使用 Windows。我该如何解决?请帮忙!

大括号 [] 具有特殊含义,如 *

[...] Matches a range of characters, similar to a RegExp range. If the first character of the range is ! or ^ then it matches any character not in the range.

所以你需要使用 \

来转义它们
const glob = require('glob')

const path = 'E:/files/Example \[Folder\] 1'
const files = glob.sync(path + '/**/*', { 
  nobrace: true,
  noext: true
})

但在您的情况下,您最喜欢寻找 rootcwd

cwd the current working directory in which to search. Defaults to process.cwd().

const path = 'E:/files/Example [Folder] 1'
const files = glob.sync('**/*', { 
  nobrace: true,
  noext: true,
  cwd: path
})

root The place where patterns starting with / will be mounted onto. Defaults to path.resolve(options.cwd, "/") (/ on Unix systems, and C:\ or some such on Windows.)

const path = 'E:/files/Example [Folder] 1'
const files = glob.sync('/**/*', { 
  nobrace: true,
  noext: true,
  root: path
})