我有一个字符串
pow((sin),(2))(4*x+5)+pow((cos),(3))(4*x+3)/pow((tan),(5))(x)
Run Code Online (Sandbox Code Playgroud)
我需要得到它
pow(sin(4*x+5),2)+pow(cos(4*x+3),3)/pow(tan(x),5)
Run Code Online (Sandbox Code Playgroud)
我试过的是
1)将基于运算符(+, - ,/,*)的表达式在pow之间拆分为单个单元.
2)提取最后一个parenthisis之间的表达
3)将提取的子表达式插入pow之后的第一个字符串和第一个关闭所有单位的parenthis之间.
我试过的: -
re="pow((sin),(2))(4*x+5)+pow((cos),(3))(4*x+3)+pow((tan),(5))(x)";
re.split(+);
re.replaceAll("^[()]]{3}","\\(\\)]*\\)");
Run Code Online (Sandbox Code Playgroud)
坦率地说,我是正则表达的新手.
小智 6
如果您的计划是评估它,而不是通过词法转换它,那么您可以通过定义适当的函数来实现:
function pow(operator, exponent) {
return function(operand) {
return Math.pow(operator(operand), exponent);
};
}
var sin = Math.sin, cos = Math.cos, tan = Math.tan;
var x = 2;
> pow((sin),(2))(4*x+5)+pow((cos),(3))(4*x+3)/pow((tan),(5))(x)
< 0.17654033709528213
Run Code Online (Sandbox Code Playgroud)