I’m very new to regex, and haven’t had luck with properly selecting/replacing these strings because of the special characters involved.
Given a form element like this:
<select id="referralForm_currentReferredProducts_1__soldProductId" class="autoWidth soldProductSelect" name="currentReferredProducts[1].soldProductId">
How can I select the id and name attributes and increment them? Form elements are being appended to a form as HTML snippets, and I don’t have the ability to implement them until they are attached to the DOM.
Thanks!
Update:
I’ve tried using regexpal to test different expressions, this one selects the underscores correctly:
\_[0-9]+\_
It just isn’t actually replacing anything.. here is my code:
$.each($('#add-a-product-wrapper').children().last().find('[id*="__"]'), function(){
$(this).attr('id').replace(/\_[0-9]+\_/, '_'+ replaceIndex +'_');
});
I was able to figure it out..
What I didn’t realize is that replace() returns a value, instead of setting something directly. once a value is returned, it can be set as the new ID of the form element.