Sunday, 8 September 2013

javascript regular expression to replace Math.cos trouble

javascript regular expression to replace Math.cos trouble

I want to replace some math functions with Math.sin. This is what I've
tried. I expect the results to be that Math.cos would be replaced by
Math.sin
re=/[Math\.cos|Math\.tan]/g;
s="Math.cos(0)+Math.cos(0)";
s=s.replace(re,"Math.sin");
alert(s);
The result I would expect would be Math.sin(0)+Math.sin(0) instead it is
Math.sinMath.sinMath.sinMath.sinMath.sinMath.sinMath.sinMath.sin(0)+Math.sinMath.sinMath.sinMath.sinMath.sinMath.sinMath.sinMath.sin(0)
I'm not sure what to try to fix the code.

No comments:

Post a Comment