1

I use .replace(/([0-9])/, '$1' + 1); for string for replace number to number + 1, but doesn't work, I know it's string and integer, but how to this in JS?

1
  • that would evaluate as .replace(/([0-9])/, '$11'); which is probably not what you want, you'd need to use a function to perform the replacement that you're after. Commented Oct 4, 2013 at 18:16

1 Answer 1

4

You cannot perform math inside a replacement string.

Instead, you need to pass a callback function:

.replace(/0-9/, function(m) { return parseInt(m, 10) + 1; })
Sign up to request clarification or add additional context in comments.

1 Comment

/\d/ to be even more concise, not sure if OP would want the g flag on there either.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.