Ashish
Ashish

Reputation: 6929

Increment numerical value inside the string

Facing issue with incrementing value in which i have to increment numerical string value.

var text = "SYN00099";
var getPart = text.replace(/[^\d.]/g, ''); // returns 00099
var num = parseInt(getPart); // returns 99
var newVal = num + 1; // returns 100
var reg = new RegExp(num.toString()); // create dynamic regexp
var newstring = text.replace(reg, newVal.toString()); // returns SYN000100
console.log(num);
console.log(newVal);
console.log(reg);
console.log(newstring);

this what i tried in my code. This works properly but the problem with this code it increase the string length if number going to increase. as example if it's last digit is 9 then it changes to 10 but it does not remove 1 zero from the string.

Upvotes: 2

Views: 633

Answers (4)

Inspiraller
Inspiraller

Reputation: 3806

Just use a return function in your regex

"SYN00099".replace(/(0*)(\d+)/, ($0, $1, $2) => $1 + (Number($2) + 1));

To ensure fixed number of 5 for example. it will only every go upto 10 if its 9. So replace 09 with the new number to keep the fixed number. Although be mindful that if your number goes upt to SYN99999 it won't update anymore as that's your max

"SYN00099".replace(/(09+$|[1-8]\d*|0$)/, ($0, $1) => Number($1) + 1);

Its better to use a functional approach like this as its more descriptive, encapsulated in one line and ultimately less boilerplate code.

Upvotes: 3

VLAZ
VLAZ

Reputation: 29096

You can use String#replace and pass a function in order to handle any where a number might appear within the string:

function increaseNumberInString(text) {
  return text.replace(
    /(\d+)/g, //match any sequence of digits
    function(match, value) {
      const num = parseInt(value); //convert to a number

      return String(num + 1) //increase by one and turn to a string
        .padStart(value.length, "0");  //pad with zeroes back to the initial length
    });
}

console.log("SYN00078 ->", increaseNumberInString("SYN00078"));
console.log("SYN00079 ->", increaseNumberInString("SYN00079"));
console.log("SYN00098 ->", increaseNumberInString("SYN00098"));
console.log("SYN00099 ->", increaseNumberInString("SYN00099"));

console.log("00078ZZ ->", increaseNumberInString("00078ZZ"));
console.log("00079ZZ ->", increaseNumberInString("00079ZZ"));
console.log("00098ZZ ->", increaseNumberInString("00098ZZ"));
console.log("00099ZZ ->", increaseNumberInString("00099ZZ"));

console.log("SYN00078ZZ ->", increaseNumberInString("SYN00078ZZ"));
console.log("SYN00079ZZ ->", increaseNumberInString("SYN00079ZZ"));
console.log("SYN00098ZZ ->", increaseNumberInString("SYN00098ZZ"));
console.log("SYN00099ZZ ->", increaseNumberInString("SYN00099ZZ"));

console.log("SYN00078AA079XX ->", increaseNumberInString("SYN00078AA079XX"));
console.log("SYN00079AA098XX ->", increaseNumberInString("SYN00099AA078XX"));
console.log("SYN00078AA099XX ->", increaseNumberInString("SYN00078AA099XX"));
console.log("SYN00099AA078XX ->", increaseNumberInString("SYN00099AA078XX"));
console.log("SYN00098AA099XX ->", increaseNumberInString("SYN00098AA099XX"));
console.log("SYN00099AA098XX ->", increaseNumberInString("SYN00099AA098XX"));

Upvotes: 2

Mr. Polywhirl
Mr. Polywhirl

Reputation: 48733

You could match the number on the end, get the length, parse it, increment, and then return the modified version with the prefix.

const increment = (str) => {
  const [ , prefix, version ] = str.match(/^(\D+)(\d+)$/);
  if (version) {
    const padding = version.length, numeric = parseInt(version, 10);
    return `${prefix}${('' + (numeric + 1)).padStart(padding, '0')}`
  } else {
    return str;
  }
};

console.log(increment('SYN00099'));

Here it is in one line:

const increment = (str) => 
  (([ , prefix, version ]) =>
    version
      ? ((padding, numeric) =>
          `${prefix}${('' + (numeric + 1)).padStart(padding, '0')}`)
        (version.length, numeric = parseInt(version, 10))
      : str)
  (str.match(/^(\D+)(\d+)$/));

console.log(increment('SYN00099'));

Upvotes: 2

Derek Wang
Derek Wang

Reputation: 10204

After having the increment, you can fill '0' in front of the num + 1 string by using String.padStart function as follows.

var newVal = (num + 1).toString().padStart(getPart.length, '0'); // returns 00100

Using that function, it will fill 0 two times in front of 100 string (so the total length = 5 = getPart string length)

Below is working example.

var text = "SYN00099";
var getPart = text.replace(/[^\d.]/g, ''); // returns 00099
var num = parseInt(getPart); // returns 99
var newVal = (num + 1).toString().padStart(getPart.length, '0'); // returns 00100
var newstring = text.replace(getPart, newVal); // returns SYN000100
console.log(num);
console.log(newVal);
console.log(newstring);

Upvotes: 2

Related Questions