Laumin
Laumin

Reputation: 1

How do I add value to all integers of a string in javascript?

I have a string and I'm trying to add a value to all numbers within that string. the string looks like this: "AXE15!io68"

How do I make it look like: "AXF26!io79"

I am new to Javascript and would love if you guys could help make this code. thank you

Upvotes: 0

Views: 29

Answers (1)

adiga
adiga

Reputation: 35222

You could use \d+ match all the numbers and replace them with the incremented number. Here match will be a string. So, you need to convert it to number using the unary plus operator before incrementing

const str = "AXE15!io68";

const output = str.replace(/\d+/g, match => +match + 11);

console.log(output)

Upvotes: 2

Related Questions