0

I have like 120 img in HTML, that are loaded properly with Javascript when click event occurs.

Then i get the attribute "src" from the img and store it in var currentModalImg_src.

For example, i receive the image number 10, and i have this string :

    var currentModalImg_src = "img/reclamos-sinaleticas/Full/f(10).jpg";

Then i extract only the number and store it in this variable: var imgSrcNumber

Then i decrement that value by 1 and replace the string with that value.

But with this code:

     currentModalImg_src = currentModalImg_src.replace( /\d/g, imgSrcNumber-1);

Im getting this (it's replacing the two digits):

img/reclamos-sinaleticas/Full/f(99).jpg

The result i want is this :

    `var currentModalImg_src = "img/reclamos-sinaleticas/Full/f(9).jpg";

For numbers like 8 or 23 (examples) works fine, the problem is when it's 10 or 100 for example.

Is there an efficient way solve this? Thanks for your attenttion.

1 Answers1

1

You are only replace one digit with a specific value, you need to replace the entire number (10, 23 or 100) with the specific value.

Replace \d with \d+

 currentModalImg_src = currentModalImg_src.replace( /\d+/g, imgSrcNumber-1);
gurvinder372
  • 66,980
  • 10
  • 72
  • 94