Let's consider we have a string named str, which is defined as :
var str = "I want to replace( this & ( this ) )"
Now, I did something like this :
str = str.replace(/replace\((.*?)\)/gm, function(_, a) {
console.log("Replacing : " + a)
return "it !"
}
Output :
// In Console
Replacing : this & ( this
// Returned
I want to it ! )
But, I wanted the output as :
// In Console
Replacing : this & ( this )
// In Return
I want it !
I heard about the Balanced Parenthesis Algorithm. Therefore can this help me to solve this task ? If yes, how ? What if, there are more brackets in the string ? If no, how can this be done ?