javascript - Node glob 中的方括号出现问题

标签 javascript node.js 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
})

关于javascript - Node glob 中的方括号出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52803272/

相关文章:

在 Dojo 构建中用作函数的 JavaScript 关键字

javascript - imacros javascript多个for循环内循环过程提取数据

javascript - 如果所有元素(表格)的宽度与某些(200)像素相同,如何隐藏它们?

node.js - 当一个特定的时候沉默 JSHint

php - 使用 PHP 缓存变量?

node.js - Nodejs glob 多种模式

javascript - 添加工厂时出现错误 : [$injector:unpr]

node.js - 请求 https ://registry. npmjs.org/co 失败

javascript - Node.js SFTP 客户端

python - 在 glob.glob() 中排除两个目录