regex - Regular Expression to replace sub string in last parenthisis to strings between trignometric fun and first right closing parenthesis in Javascript -
i have string
pow((sin),(2))(4*x+5)+pow((cos),(3))(4*x+3)/pow((tan),(5))(x)
i need as
pow(sin(4*x+5),2)+pow(cos(4*x+3),3)/pow(tan(x),5)
what tried was
1)split expression based on operators(+,-,/,*) between pow single units.
2)extract expression between last parenthisis
3)insert extracted subexpression between first string after pow , first closing parenthis units.
what tried :-
re="pow((sin),(2))(4*x+5)+pow((cos),(3))(4*x+3)+pow((tan),(5))(x)"; re.split(+); re.replaceall("^[()]]{3}","\\(\\)]*\\)");
to frank new regular expression.
you can use following regex , replacement string:
edited
regular expression : ([a-z]+)\(\(([a-z]+)\),\(([0-9]+)\)\)\(([a-z0-9\*\+]+)\)([\*\-%\/+]*)
replacement expression : $1($2($4),($3))$5
check following code:
function cal() { var string = "pow((sin),(2))(4*x+5)+pow((cos),(3))(4*x+3)/pow((tan),(5))(x)"; var regex = /([a-z]+)\(\(([a-z]+)\),\(([0-9]+)\)\)\(([a-z0-9\*\+]+)\)([\*\-%\/+]*)/; var replacement = "$1($2($4),($3))$5"; while(string.match(regex)) { string = string.replace(regex,replacement); } alert(string); } cal();
Comments
Post a Comment