Well, first of all you can define the regular expression with its own constant syntax:
var reg = /arrayname\[1\]/;
Inside the regular expression you quote things with backslash. Now, if you're starting from a string, you have to "protect" those backslashes inside the string constant. In that case, the pattern is being parsed twice: once when the string constant is gobbled by the Javascript parser, and then once by the RegExp constructor:
var pattern = "arrayname\\[1\\]";
var reg = new RegExp(pattern);
The backslashes are doubled so that the string "pattern" will look like the regular expression in my first example - one backslash before each bracket character.