Do this using the callback function, where you can get the value of the old attribute as the second argument.
$('.el').attr('src',function(i, oldSrc){
return oldSrc.replace('/subpath1/', '/subpath2/');
})
With function ES6 arrow
$('.el').attr('src', (i, s) => s.replace('/subpath1/', '/subpath2/'))
You can reduce the length of the code using the regular expression of the capture group.
$('.el').attr('src', (i, s) => s.replace(/(\/subpath)1\//, '$12/'))
source
share