在 Node JS 的 Google 云计算库中,如何在 getVMs
方法中使用带有 options.filter
的正则表达式进行过滤?
文档:https://googlecloudplatform.github.io/google-cloud-node/#/docs/compute/0.7.1/compute?method=getVMs
我下面的尝试返回一个空数组,并且文档中没有明确的示例(还!)。我的目标是获得一个以“prefix”作为名称开头的实例。
共有 101 个实例,这是第 101 个。
compute.getVMs({
maxResults: 100,
filter: 'name eq ^prefix'
});
Search filter in the format of {name} {comparison} {filterString}. name: the name of the field to compare comparison: the comparison operator, eq (equal) or ne (not equal) filterString: the string to filter to. For string fields, this can be a regular expression.
最佳答案
您的模式末尾缺少“.$”。基本上,您需要指定您想要“匹配以“前缀”开头的字符串,然后在“前缀”之后跟随任意数量的字符,直到整个字符串的末尾”。您可以抑制“整个字符串的结尾”特殊字符 ($),但不能抑制“匹配任意数量的字符”特殊字符 (.)。这是一个工作示例:
const Compute = require('@google-cloud/compute');
const gce = new Compute({
projectId: 'your-project-id-here'
});
gce.getVMs({
maxResults: 100,
filter: 'name eq ^prefix.*'
}, function(err, vms) {
console.log(vms);
});
关于javascript - 如何在 Google Cloud Compute node.js getVMs 中使用正则表达式进行过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44081916/