0

Could you help me out how to replace anything between "666666&r=" and "&u" in the img src below ?

img src="img.php?&w=300&b=a&n=666666&r=3m&u=domain.com" class="graph"

Existing attempt

$("#changeimg").change(function() {

    $('.graph').each(function(index) {

       //var sub = t.replace(/^.*666666&r=(.*)&u=*$/, '1y'); ??

       $(this).attr("src", sub);

    });

});

4 Answers 4

2

This will work nicely:

var newString = oldString.replace(/([&?]r=)[^&]*/, '$11y');

It will replace the r= parameter regardless of whether it is at the beginning of the querystring, or in the middle (like in the example you posted)

Sign up to request clarification or add additional context in comments.

Comments

1
t.replace( /^(.*666666&r=)([^&]*)(&u=.*)/, "$11y$3" )

Example and proof at https://regex101.com/r/M8nDNA/1

Comments

0
var sub = $(this).attr('src').replace(/&r=.*&/, '&r=1y&');

Comments

0

Thank you all for the answers !!!

This is working now :

$("#changedate").change(function() {
    var sel = $("#changedate").val();
    $('img').each(function(index) {
        $(this).attr("src", $(this).attr('src').replace(/([&?]r=)[^&]*/, '$1' + sel));
    });
});

Comments

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.