4

I am adding a new field in one of my web apps, for visitors to add a product. Pushing "add a product" clones one of the existing inputs, and I set it to a blank value.

I need to update the name however. The name is in this format:

<input type="text" name="product[0][0][3][title]" value="my product" id="input-AbGHtQS3" maxlength="150" />

I need to increment the last numerical index, that is the 3 in this example.

I wrote a regex to match the appropriate character, but how can I increment that last number?

Here is my regex:

/^product\[\d+\]\[\d+\]\[(\d+)\]\[.+\]/

How could I increment that last number?

KatieK
  • 13,586
  • 17
  • 76
  • 90
alex
  • 479,566
  • 201
  • 878
  • 984

3 Answers3

10

from: Use RegExp to match a parenthetical number then increment it

The replace method can take a function as its second argument. It gets the match (including submatches) and returns the replacement string. Others have already mentioned that the parentheses need to be escaped.

"Item Name (4)".replace(/\((\d+)\)/, function(fullMatch, n) {
    return "(" + (Number(n) + 1) + ")";
});

So,

*edit:

this should work

"product[0][0][3][title]".replace(/(^product\[\d+\]\[\d+\]\[)(\d+)(\]\[.+\])/, function(fullMatch, n, a, o) {
    return n + (Number(a) + 1) + o;
});
Community
  • 1
  • 1
Andrew Mcveigh
  • 474
  • 5
  • 8
  • 1
    Didn't know you could give an anonymous function as 2nd argument! Learn something new every day. Thanks a bunch, will implement now. – alex Dec 16 '09 at 05:52
  • Couldn't get it to work - see http://jsbin.com/obila/edit it is returning 4 - is that correct? How would I then make the name attribute read correctly? i.e. 'product[0][0][4][title]' – alex Dec 16 '09 at 06:04
  • Ah crap, it's replacing the whole match. I'll give it another try. – Andrew Mcveigh Dec 16 '09 at 06:17
1

A more iterative approach:

r = /^(product\[\d+\]\[\d+\]\[)(\d+)(\]\[[a-z]+\])$/;
m = s.match(r);
if (!m) { /* do something smart; */ }
s.replace(r, "$1" + (Number(m[2]) + 1) + "$3");

I added a capture group at the head and tail of your regex. Then match, increment, and reassemble the string.

+1 for the match function on the regex though, I didn't know that trick.

Seth
  • 45,033
  • 10
  • 85
  • 120
1

Based on Andrew's answer, but corrected to return entire string:

"product[0][0][3][title]".replace (
    /^(product\[\d+\]\[\d+\]\[)(\d+)(\]\[.+\])/,
    function(fullMatch, pre, n, post) {
        return pre + (Number(n) + 1) + post;
    }
);
Community
  • 1
  • 1
K Prime
  • 5,809
  • 1
  • 25
  • 19