Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 986383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:18:42+00:00 2026-05-16T05:18:42+00:00

I’m trying replace the index of form elements. I have the following var test

  • 0

I’m trying replace the index of form elements. I have the following

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

I’m getting curious results. The first hidden field is replaced by the second is ignored

ie my response is something like this:

"<input name='[1].Id' value='598' type='hidden' /><input name='[2].OrderItemId' value='867' type='hidden' />"

EDIT:

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 “

<td>

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

                </td>
               <td class="p-5" width="100">
               <select name="[1].AddressId"><option value="2">address1</option></select>                            
                </td>

and here is the js function

    $('#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('<tr>' + newRow + '</tr>');
        AdjustValues();
    });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:18:43+00:00Added an answer on May 16, 2026 at 5:18 am

    In JavaScript, passing a string as the first parameter to replace() will only replace the first occurrence. You need to use a regex, with the global flag:

    test.replace(/\[1\]/g, '[2]');
    

    The extra backslashes (\) escape the brackets ([ and ]).


    Edit: responding to the OP’s edit, if you want to dynamically build a regex, you can’t use a regex literal – that’s the thing delimited by forward slashes (/), not quotes (") as in your edit. You’re passing a string into replace(), I’m passing in a regex literal. Use the JavaScript RegExp() constructor to fix yours:

    // The first argument is the regex, the second is a string of flags.
    var previousIndex = new RegExp("\\[" + previousLength + "\\]", "g");
    
    // Then, it's exactly the same as before.
    // The second argument to replace is still a string.
    var newRow = lastRow.html().replace(previousIndex, currentIndex);
    

    Note the difference in character escaping.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.