[英]Is it guaranteed that `Math.cos(0) === [Math.cos][0](0)`?
[英]javascript regular expression to replace Math.cos trouble
我想用Math.sin
替换一些math
函数。 这就是我尝试过的。 我希望结果是Math.cos
将被Math.sin
取代
re=/[Math\.cos|Math\.tan]/g;
s="Math.cos(0)+Math.cos(0)";
s=s.replace(re,"Math.sin");
alert(s);
我期望的结果是Math.sin(0)+Math.sin(0)
而是
Math.sinMath.sinMath.sinMath.sinMath.sinMath.sinMath.sinMath.sin(0)+Math.sinMath.sinMath.sinMath.sinMath.sinMath.sinMath.sinMath.sin(0)
我不确定该如何解决该代码。
方括号从根本上改变了正则表达式的含义。 摆脱它们。
re = /Math\.cos|Math\.tan/g;
您有方括号(表示匹配方括号之间的任何字符),而不是括号(表示将这些不同的表达式归为一组,以匹配方式返回),请尝试:
re=/(Math\.cos|Math\.tan)/g;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.