JSFiddle - React, Tailwind, and code Playground

by Soviut

CSS

body {
  background: black;
}

JavaScript

//const invoiceId = 'mb-00-0001b'
const invoiceId = 'aaa'


// start at end and go backwards
// find first number
// find last contiguous number
// extract
// increment
// slice from start, insert, slice to end

const match = invoiceId.match(/\d+(?=\D*$)/)

if (match) {
  const rawNum = match[0]
  const num = parseInt(rawNum, 10)
  
  const leading = invoiceId.slice(0, match.index)
  const trailing = invoiceId.slice(match.index + rawNum.length)
  
  const output = leading + `${num + 1}`.padStart(rawNum.length, '0') + trailing
  
  console.log(leading, trailing, output)
  
} else {
  // no numbers found to increment, append 1 to the end
	console.log(`${invoiceId}1`)
}