Is there someone to help me with my regex?
I want to match always last integer suquare bracket for every string.
product[attribute][1][0][value] - In this case [0]
product[attribute][9871][56][value] - In this case [56]
/\[[0-9,-]+\]/g
The goal is to increment input name on clone, product[attribute][{attribute_id}][{clone_index}][value]
.
You may use
var s = "product[attribute][1][0][value]";
console.log(s.replace(
/(.*\[)(\d+)(?=])/, function($0, $1, $2) {
return $1 + (Number($2)+1);
})
)
The regex matches
(.*\[)
- Group 1: any 0+ chars other than line break chars as many as possible and then [
(\d+)
- Group 2: one or more digits (?=])
- a ]
char must appear immediately to the right of the current location.Incrementing is done inside the callback method.