日期:2014-05-16 浏览次数:20355 次
var s = '($目标值+$权重-0.2)*100/20+10'; s = s.replace(/\$/g, ''); //去掉$符号 var ar = s.split(''); function in_array(k, arr) { for (var i = 0; i < arr.length; i ++) if (arr[i].toString() == k) return true; return false; } var sep = ['(', ')', '+', '-', '*', '/']; var result = [], flag = false; //flag变量指示上一个字符是否为分隔符 for (var i = 0; i < ar.length; i ++) { if (in_array(ar[i], sep)) { result.push(ar[i]); flag = true; } else { if (flag) result.push(ar[i]); else result.length == 0 ? result.push(ar[i]) : result[result.length - 1] += ar[i].toString(); flag = false; } } alert(result);