2 回答
TA贡献1831条经验 获得超4个赞
var str = 'a>b and b<c and c=d';
var expressions = str.split('and');
var regExp = /[+-\\*%/><=]/;
var expArray = [];
for(var i=0;i<expressions.length;i++){
var expression = expressions[i];
var keyValue = expression.split(regExp);
var item = {};
item.key = keyValue[0];
item.value = keyValue[1];
item.symbol = expression.charAt(expression.search(regExp));
expArray.push(item);
}
console.log(JSON.stringify(expArray));
TA贡献1802条经验 获得超6个赞
console.log(str.split('and').map(v => v.trim()).reduce((pre, item) => {
const reg = /(.*)([>=<])(.*)/g;
const [t, name, symbol, value] = reg.exec(item);
return [...pre, {name, symbol, value}];
},[]));
添加回答
举报