I'm trying to build an html form that generates structured JSON code from the input values. This form uses repeat fields. I have to modify name attributes in cloned fields.
In Javascript I have a string 'menu-item[0][offers][0][price]'. I would like to replace 'menu-item[0]' with 'menu-item[1]', as in example at http://regexr.com/3gpnt I'm using RegExp, but is not required.
This is my experiment, but it doesn't work.
var string = 'menu-item[0][offers][price]';
var itemName = 'menu-item';
var regExp = new RegExp(itemName + '\[(.*?)\]', "");
var newString = string.replace(regExp, itemName + '[1]');
console.log(newString);
alert(newString);
Returns 'menu-item[0][offers][price]'.
Test on jsfiddle https://jsfiddle.net/lorenzodetomasi/dmnd7f9L/
Thank you.