I need to convert some variables that use parentheses instead of bracket in a Javascript file. For example, MyVariable(i) should be MyVariable[i].
I use the Find and Replace tool of Visual Studio with this Regex :
MyVariable\({(.+)}\)
and replace with:
MyVariable\[\1\]
This work fine for case like :
asdas.MyVariable(i+1)
asdas.MyVariable(i)
asdas.MyVariable(i).asd
MyVariable(i+1)
But doesn’t work for case like
if (parseInt(OtherObject.MyVariable(i+1).Dest.XYZ)==SINK_STATE_TYPE || OtherObject.MyVariable(i).X == "ok")
The last one will do take the first parentheses and the last one of the line and act weird. What do I need to change in the Regex to be able to make it works with line that has multiple parentheses.
Your pattern is greedy. The
(.+)part matches everything, including a closing parenthesis)character until it reaches the last)character. To make it non-greedy you can use#instead of+, which means “match one or more occurrences of the preceding expression, and match as few characters as possible.” In .NET this is usually handled by placing a?after it, such as.+?but the Visual Studio regex flavor is different and uses the#metacharacter instead.The updated pattern becomes:
You can also use
[^)]+, which matches any character that is not a)character. When it encounters a)it will stop matching. This alternate pattern is:If you’re familiar with the .NET regex flavor you might be interested in checking out the Visual Studio 2010 Productivity Power Tools, which has extended Find/Replace to allow the .NET regex flavor to be used.