我正在努力寻找一种方法来获得变量的模式匹配。我曾尝试拆分事物并执行 indexOf、使用 match 和 switch (true) 语句,但都收效甚微。任何帮助,将不胜感激!
currentExports = 'sec=sys,rw=badhost1.foo.com:badhost2.foo.com,root=badhost1.foo.com:badhost2.foo.com';
badExportHosts = params.badExportHosts.split(':');
for (badHost = 0; badHost < badExportHosts.length; badHost++) {
if (!currentExports.match(/badExportHosts[badHost]/g)) {
printf('Entry ' + badExportHosts[badHost] + ' was not found in ' + currentExports + '\n');
} else {
printf('Entry ' + badExportHosts[badHost] + ' was found in ' + currentExports + '\n');
}
如果我输入我的表格:
badhost1.foo.com:badhost2.foo.com
我不幸得到了这个结果:
Entry badhost1.foo.com was not found in sec=sys,rw=badhost1.foo.com:badhost2.foo.com,root=badhost1.foo.com:badhost2.foo.com
Entry badhost2.foo.com was not found in sec=sys,rw=badhost1.foo.com:badhost2.foo.com,root=badhost1.foo.com:badhost2.foo.com
我怎样才能让这个匹配?