0

I may have following type strings

A1 or 1A or AB....1 or 1AB......

so how to increment only digits of above type of strings in javascript?

var adminNo = data.Admission_No.slice(-2);
                alert(adminNo);
                var removedNo = data.Admission_No.substring(data.Admission_No.length-1);
                alert(removedNo);
Dawood Abbas
  • 222
  • 2
  • 12

3 Answers3

4

Use the replace method as shown in demo below

    function incrementer(input)
    {
       return input.replace(/\d+/, function(match){ return parseInt(match) + 1 });
    }

    alert(incrementer("A1"));
    alert(incrementer("1A"));

This will find the integer anywhere in the input string and increment it by one.

gurvinder372
  • 66,980
  • 10
  • 72
  • 94
3
string.replace(/\d+/, function(n){ return ++n });
Flash Thunder
  • 11,672
  • 8
  • 47
  • 91
0

You can do it by taking out integer from your string Its big long, but more self-explainatory

var youroriginalstring="A1"
var withNoDigits = youroriginalstring.replace(/[0-9]/g, '');
var yournumber = youroriginalstring.replace ( /[^\d.]/g, '' );
var incNos=yournumber +1;
var newString = incnos + "withNoDigits"
Abhishek
  • 517
  • 3
  • 18