console
function cal(str) {
str = str.replace(/\s/g, '')
let arr = [...str]
let stack = []
let res = 0
let cal = []
for (let i = 0; i < arr.length; i++) {
if (arr[i] === '(') {
stack.push(arr[i])
} else if (arr[i] === ')') {
cal = []
let cur = stack.pop()
while (cur !== '(') {
cal.unshift(cur)
cur = stack.pop()
}
cal = cal.toString().replace(/\,/g, '')
console.log(cal)
res = eval(cal)
stack.push(res)
} else {
stack.push(arr[i])
}
}
return res
}
console.log(cal('((2 + (3 * 2) ) * (2 + 3) + (3 - 1) )'))
<div class="red blue green">hello world</div>
.red {
color: red;
}
.green {
color: green;
}
.blue {
color: blue;
}