【发布时间】:2018-10-19 11:08:58
【问题描述】:
在 JavaScript 中,你可以这样做:
someComplexProcessing = (wholeMatch, group1, group2, index, mystr)=> replacement...
mystr.replace(/some.* regex(with) multiple (capture groups)/g, someComplexProcessing)
例如。
const renderTemplate = (str, env)=> str.replace(/{{(.*?)}}/g, (_, name)=> env[name])
renderTemplate('{{salut}} {{name}}!', {salut: 'Hi', name: 'Leo'}) // "Hi Leo!"
最好的 POSIX 兼容、通用、变体是什么?
- reusability # eg. a function taking regex, processingFunction, and input, etc - that I could but in my .shellrc/source lib.sh or similar and reuse
- multiline # eg. if "uppercase everything between {{ and }}", `a {{b\nc}}` -> `a B\nC`
- no escape gotchas # eg. it shouldn't break if input, replacement, or regex contains special characters
- POSIX compatible # eg. running it under `docker run --rm -it alpine sh`, etc
- using regex # eg. perl regex seems like the most prominent one, please note differences from it if other is used
meriting:
- no/less dependencies # eg. as portable as possible
- multiple capture groups
- performance
- security # related to no escape gotchas, eg. ok with untrusted input
我找到了一些 bash 的解决方案,以及一些兼容的边缘案例解决方案,尽管没有一个能够完全接近 js 的 .replace 提供的简单性。最终,我想在不考虑实现细节/陷阱的情况下进行编程,并且不引入 100 的 MB(主要是 alpine 容器,但也使用 ubuntu/OSX),从而尝试建立一个可移植的、与 posix 兼容的库sn-ps、函数和模式。
【问题讨论】:
-
您可以查看
expr命令,但您不应该期望shell 语言提供通用编程语言提供的数据操作类型。 Shell 被定义为运行用(例如)JavaScript 编写的程序,而不是替换那些程序。 -
澄清一下:我可以访问的起点/环境是 POSIX 兼容的。我的最终目标是 运行 来自 sh 的查找和替换的东西 - 即。它可以是一个编译的 c 程序,只要它满足上述一些点——尽管不是 JavaScript,因为 Node 大于 50MB。此外,每场比赛都将通过另一个程序传递,这使得 sh 看起来是一个不错的选择(无论如何它是开始和中间的一部分)。