Patrick Desjardins Blog
Patrick Desjardins picture from a conference

How to convert Javascript parenthese to access array to square bracket?

Posted on: 2011-11-11

It can happen in old project that array objet are accessed with parentheses instead of square bracket.

For example, MyArray[0] is in fact the first element of an array in Javascript. But, IE let you use MyArray(0). This is not a good practice and other browsers doesn't accept this syntax.

To convert easily, you can use a Regex expression. In my case, the array name was InTran.

InTran\\({(.+)}\\) //Find

InTran\\[\\1\\] //Replace

The curly bracket is required by Visual Studio to have a backreference but is not required by all Regex tool.