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

129
Views
Replace only the Second number from a string

How do i replace the second number from a string only?

Actual sting: messages_0_items_0_data_0

Required Results: messages_0_items_1_data_0

var new_index = 1;
this_input.attr('id', this_input.attr('id').replace(/[0-9]+(?!.*[0-9])/, new_index ));

the above code is changing the last number rather than the second number like follwoing:

Not Good: messages_0_items_0_data_1

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

0

You could capture just before the second number in group 1, and then match the second number to be replaced.

^(\D*\d+\D+)\d+

See a regex demo.

const s = `messages_0_items_0_data_0
messages[0][items][0][data][0]`;
const regex = /^(\D*\d+\D+)\d+/gm;
console.log(s.replace(regex, (m, g1) => `${g1}1`));

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!