【问题标题】:Having problem with square brackets in node glob节点 glob 中的方括号有问题
【发布时间】:2018-10-14 13:43:31
【问题描述】:

我正在使用这个包node-glob

我面临的问题是,只要我的路径包含方括号 [],它就不会给我任何文件。

这就是我的做法:

const glob = require('glob')

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

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

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

【问题讨论】:

  • 我讨厌成为明显解决方案的承担者,但文件/文件夹名称是否需要包含 [ ] 字符?虽然它是完全有效的,但它似乎非常规。
  • 不应该是 E:// 加双斜线吗?
  • 实际上,我正在开发一个让用户下载文件的下载器。一些文件夹包含 [ ]。所以我需要一个解决方案。
  • @ZhongWang 它适用于单斜杠。

标签: javascript node.js glob


【解决方案1】:

大括号[] 有一个特殊的含义,比如*

[...] 匹配一系列字符,类似于 RegExp 范围。如果范围的第一个字符是 !或 ^ 则匹配不在范围内的任何字符。

所以你需要使用\来逃避它们

const glob = require('glob')

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

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

cwd 要搜索的当前工作目录。默认为process.cwd()

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

root/ 开头的模式将被挂载到的位置。默认为path.resolve(options.cwd, "/")(Unix 系统上为/,Windows 上为C:\ 或类似的。)

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

【讨论】:

  • 非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-11
  • 2019-06-28
  • 2014-05-24
  • 1970-01-01
  • 1970-01-01
  • 2014-04-09
  • 1970-01-01
相关资源
最近更新 更多