一个实现把公式中的变量进行任意替换的JS
今天同事阿聪他需要实现一个把一个运算公式中的变量替换的功能,我就帮忙想一下,写了下面这么一个方法来测试,好像OK。
<script type="text/javascript">
function test(){
var newV = "";
var arr;
var maths = ["+", "-", "*", "/", "(", ")" ];
var temp = "abc,+,bcd,*,(,cde,-,def,)-ttt/bv,";
arr = temp.split(",");
for(var j=0; j<(arr.length-1); j++)
{
var a1 = "";
for(var m=0; m<maths.length; m++)
{
if(arr[j]==maths[m])
{
a1 = arr[j];
}
}
if(a1 == "")
{
var t = arr[j];
a1 = "innerHTML"+t;
}
//alert(newV)
newV=newV+a1;
}
alert(newV);
}
</script>
function test(){
var newV = "";
var arr;
var maths = ["+", "-", "*", "/", "(", ")" ];
var temp = "abc,+,bcd,*,(,cde,-,def,)-ttt/bv,";
arr = temp.split(",");
for(var j=0; j<(arr.length-1); j++)
{
var a1 = "";
for(var m=0; m<maths.length; m++)
{
if(arr[j]==maths[m])
{
a1 = arr[j];
}
}
if(a1 == "")
{
var t = arr[j];
a1 = "innerHTML"+t;
}
//alert(newV)
newV=newV+a1;
}
alert(newV);
}
</script>