我试图寻找一个可以对字符串数组运行grep过滤器的包。但我找不到一个。
我想做这样的事情:
const paths = ['some/file/paths-1', 'some/other/paths', ... ]
const filteredPaths = grep(paths, 'some/file/**/*')
我理解这个问题并不是一个好的问题,因为它可以是固执己见的,并且有利于特定的一揽子计划。
但我认为很多人会寻找同样的事情。
我找到了:https://www.npmjs.com/package/matcher
如果您从cli或config获取输入,这将非常有用。在这些情况下的正则表达式会太复杂。
有时为工作寻找合适的包装很难,今天使用https://github.com/npm/npm/issues/19438更难
我会在这里留下问题和答案,以便人们可以更轻松地找到它。
尝试使用grep来解决这个问题是没有任何意义的。 Javascript正则表达式工作正常。
const paths = ['some/file/paths-1', 'some/other/paths', ... ]
const filteredPaths = paths.filter(path => /^some\/file\/.*/)
如果你坚持使用grep,你可以这样做:
const fs = require('fs')
const execSync = require('child_process').execSync
const paths = ['some/file/paths-1', 'some/other/paths']
fs.writeFileSync('/tmp/b', paths.join('\n'), 'utf8')
const grepResult = execSync("grep 'some/file/' /tmp/b")
const filteredPaths = grepResult.toString()
console.log(filteredPaths)
我个人没有看到任何合适的情况