I have the following function in my javascript code:
addParam(url, param, value) {
var a = document.createElement('a'), regex = /(?:\?|&|&)+([^=]+)(?:=([^&]*))*/g;
var match, str = []; a.href = url; param = encodeURIComponent(param);
while(match = regex.exec(a.search)) {
if(param != match[1]) {
str.push(match[1] + (match[2] ? '=' + match[2] : ''));
}
}
str.push(param + (value ? '=' + encodeURIComponent(value) : ''));
a.search = str.join('&');
return a.href;
}
eslint returns me following error:
306:15 error Expected a conditional expression and instead saw an assignment no-cond-assign
The problem is this while statement:
while(match = regex.exec(a.search)) {
if(param != match[1]) {
str.push(match[1] + (match[2] ? '=' + match[2] : ''));
}
}
How can I rewrite this to fix that?
You want to do:
while((match = regex.exec(a.search))) {
if(param != match[1]) {
str.push(match[1] + (match[2] ? '=' + match[2] : ''));
}
}
you need that extra set of parentheses
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With