I have the following problem: I have a string and I need to replace for different regex parts.
therefore I want to loop over it:
In my case its a URL and I want to empty for specific parameters:
cleanURL(url, params) {
params.forEach((param) => {
url = this.updateUrlParameter(url, param, '');
});
Whereas I have:
updateUrlParameter(url, param, value) {
const regex = new RegExp(`(${param}=)[^&]+`);
const newURL = url.replace(regex, `$1${value}`);
return newURL;
}
This is the only way I got it to work, but now I am reassigning a function parameter. this is not what I want.
My problem is, that
cleanURL(url, params) {
params.forEach((param) => {
this.updateUrlParameter(url, param, '');
});
}
Would always for each iteration pass the SAME url
into the loop. And I end up with just the last replacement.
I somehow would have to pass a changed string into the next loop iteration.
How could I achieve this? Maybe into this direction? With somehow nesting and calling the function again?
while(outerI--){
(function(i){
i+=1;//doesn't affect outerI and you wanted 1-length so we add one.
//crap inside your forEach loop but without the i++
})(outerI)
}
which I found here: .forEach loop: use variable
Woudl be very glad for a hint here. thank you
Cheers