JavaScript not replacing all elements
<p>I'm trying replace the index of form elements. I have the following</p>

<pre><code>    var test = "&lt;input name='[1].Id' value='598' type='hidden' /&gt;&lt;input name='[1].OrderItemId' value='867' type='hidden' /&gt;";
    alert(test.replace('[1]', '[2]'));
</code></pre>

<p>I'm getting curious results. The first hidden field is replaced by the second is ignored</p>

<p>ie my response is something like this:</p>

<pre><code>"&lt;input name='[1].Id' value='598' type='hidden' /&gt;&lt;input name='[2].OrderItemId' value='867' type='hidden' /&gt;"
</code></pre>

<p>EDIT:</p>

<p>OK, thanks these methods worked on my simple example. However in reality my string is a bit more complex. Here is the contents of "var lastRow "</p>

<pre><code>&lt;td&gt;

                 &lt;a class="deleteAddress" href="#"&gt;
                 &lt;img alt="remove" src="/images/icons/delete_button.gif"&gt;
                 &lt;/a&gt;&lt;/td&gt;
                &lt;td class="p-5" width="100"&gt;
                   &lt;input name="[1].Id" value="612" type="hidden"&gt;
                   &lt;input name="[1].OrderItemId" value="868" type="hidden"&gt;
                   &lt;input class="itemAddressQuantity" name="[1].Quantity" value="" type="text"&gt;

                &lt;/td&gt;
               &lt;td class="p-5" width="100"&gt;
               &lt;select name="[1].AddressId"&gt;&lt;option value="2"&gt;address1&lt;/option&gt;&lt;/select&gt;                            
                &lt;/td&gt;
</code></pre>

<p>and here is the js function</p>

<pre><code>    $('#addNewAddress').click(function (event) {

        event.preventDefault();
        var length = $('.table-item-address tbody').find('tr').length;
       var previousLength = length - 1;
        var previousIndex = "/\[" + previousLength + "\]/g";
        var currentIndex = "[" + length + "]";
        var lastRow = $('.table-item-address tbody tr').last();
alert(lastRow.html()); // html is shown above
        var newRow = lastRow.html().replace(previousIndex, currentIndex);
        $('.table-item-address tr').last().after('&lt;tr&gt;' + newRow + '&lt;/tr&gt;');
        AdjustValues();
    });
</code></pre>
