我有一个来自数据库的计算字符串,例如:
var calc = "{a}+{b}==2"
我想用“{}”提取所有元素,以便我可以从数据库中查找它们的值。这样做的最快方法是什么,所以我最终得到一个可以查找的有序数组,并将值替换回字符串中。
我考虑过:
- For loop, looking for { then finding the next }
- Split with a map
- IndexOf
使用正则表达式
var exp = /{([^}]+)}/g ,index;
while(index = exp.exec("{a}+{b}==2")) {
console.log(index[1]);
}
. 演示
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句