Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

211
Views
Increment number in string in JS

I have this regex, but it does not do what I expect:

name.replace(/sor\s*(\d+)\.\s*szék/, function () {
  return arguments[1] * 1 + 1;
});

My string is 1. sor 1. szék. And I would expect a return 1. sor 2. szék

but the return value is: 1. 2. Why?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You get the value 1. 2 as the pattern sor\s*(\d+)\.\s*szék does not match the leading 1. so that will be untouched.

The part arguments[1] * 1 is 1, and then you add 1 to it resulting in 2.


If you want to use replace, you can use 3 capture groups and use those in the replacement, passing the group parameters to the callback function of replace.

The captured digits are in group 2, and to prevent concatenating 2 strings you can add a + before group 2.

let name = "1. sor 1. szék";
name = name.replace(
  /(sor\s*)(\d+)(\.\s*szék)/,
  (m, g1, g2, g3) => `${g1}${+g2+1}${g3}`
);
console.log(name);

about 4 years ago · Juan Pablo Isaza Report

0

You could simplify the regular experession and replace only the second occurence of digits with an incremented value.

const
    string = '1. sor 1. szék.',
    result = string.replace(/\d+/g, (c => v => c-- ? v : +v + 1)(1));

console.log(result);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!