If I have the string:
var myStr = \"foo_0_bar_0\";
and I guess we should have a function called getAndIncrementLastNumber(str)
try this demo please http://jsfiddle.net/STrR6/1/ or http://jsfiddle.net/Mnsy3/
code
existingId = 'foo_0_bar_0';
newIdOnly = existingId.replace(/foo_0_bar_(\d+)/g, "$1");
alert(newIdOnly);
getAndIncrementLastNumber(existingId);
function getAndIncrementLastNumber(existingId){
alert(existingId);
newId = existingId.replace(/(\d+)/g, function(match, number) {
return parseInt(number) + 1;
});
alert(newId);
}
or
existingId = 'foo_0_bar_0';
newIdOnly = existingId.replace(/foo_0_bar_(\d+)/g, "$1");
alert(newIdOnly);
getAndIncrementLastNumber(existingId);
function getAndIncrementLastNumber(existingId){
alert(existingId);
newId = existingId.replace(/\d+$/g, function(number) {
return parseInt(number) + 1;
});
alert("New ID ==> " + newId);
}