title: jquery change multiple IDs with regular expression
body: <p>i need to modify multiple IDs, classes and names of a div, span, select and other elements.</p>
  
  <p>i clone the html from the last row i have in a table, like</p>
  
  <pre><code>var num     = $('.clonedLighting').length; // num of current rows
  var newNum  = new Number(num + 1);      // the numeric ID of the new input field being added
  
  // create the new element via clone(), and manipulate it's ID using newNum value
  var newElem = $('#inputlighting' + num).clone().prop('id', 'inputlighting' + newNum);
  </code></pre>
  
  <p>Then in the next step i modify the cloned IDs like</p>
  
  <pre><code>newElem.children('td').children('label.prod_item').prop('for', 'appliancetextfield' + newNum);
  newElem.children('td').children('select.prod_item').prop('id', 'appliancetextfield' + newNum).prop('name', 'appliancetextfield' + newNum).prop('value', '');
  </code></pre>
  
  <p>which is working OK. however, the appliancetextfield sections now has many more elements. I would like to replace all names, classes and IDs that contain</p>
  
  <pre><code>'appliancetextfield'+num
  </code></pre>
  
  <p>with </p>
  
  <pre><code>'appliancetextfield'+newNum
  </code></pre>
  
  <p>instead of doing it for every element. is this possible? how?</p>
  
  <p>to clarify: the IDs, classes or names would look like</p>
  
  <pre><code>appliancetextfield4
  appliancetextfield4_title
  appliancetextfield4_titleText
  appliancetextfield4_msdd
  </code></pre>
  
  <p>and should turn into</p>
  
  <pre><code>appliancetextfield5
  appliancetextfield5_title
  appliancetextfield5_titleText
  appliancetextfield5_msdd
  </code></pre>
  
  <p>at this point it looks like the number will only ever be single digit, but one day may have to go up to 10 or higher. Thanks for any input.</p>
  
  <p>EDIT:
  Trying to implement the below suggestions and lots of googling:
  I now have:</p>
  
  <pre><code>var num     = $('.clonedLighting').length; // current rows
  var newNum  = new Number(num + 1); 
  
  $('#inputlighting' + num).clone().prop('id', function(index, id) {
      return id.replace(/(appliance.*)\d\d?(.*)/g, "$1" + newNum + "$2");
  }).insertAfter($('#inputlighting' + num));
  </code></pre>
  
  <p>it looks like the right track but the regular expression and/or the back-referencing doesn't work.</p>