我正在使用这个包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
})
但就您而言,您最喜欢查找 root
或 cwd
。
cwd
the current working directory in which to search. Defaults toprocess.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 topath.resolve(options.cwd, "/")
(/
on Unix systems, andC:\
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/