最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I increment a number matched via regex? - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question edited Aug 2, 2011 at 17:53 KatieK 13.9k19 gold badges78 silver badges90 bronze badges asked Dec 16, 2009 at 5:38 alexalex 490k204 gold badges889 silver badges991 bronze badges 2
  • why would you put what looks like a Javascript array in the name? Doing an eval on it later? – Murali VP Commented Dec 16, 2009 at 5:44
  • 2 So I can work with it easily in PHP as an array – alex Commented Dec 16, 2009 at 5:46
Add a ment  | 

3 Answers 3

Reset to default 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;
});

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.

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;
    }
);
发布评论

评论列表(0)

  1. 暂无评论