Forum Moderators: phranque
// string to match
str = '?id=123&h=456&s=789';
// regex to substitute
exp = /(\?|&)(h|z|s)=[^&]+&?/gi;
// result
?id=123&s=789 // string to match
str = '?id=123&h=456&s=789';
// regex to substitute
exp = /(\?|&)(h|z|s)=[^&]+/gi;
// result
?id=123&& // string to match
str = '?h=456&id=123&s=789';
// regex to substitute
exp = /(\?|&)(h|z|s)=[^&]+&?/gi;
// result
?id=123& so I end up with unnecessary & in placesI don't see why, since the pattern is, or should be, “question mark or ampersand plus the rest of the parameter-plus-value pair”. You definitely don't want to have & at both ends of the pattern--keeping in mind that Regular Expressions are greedy by nature, and will grab a & if they see one--or you could end with
(\?|&)and (h|z|s) rather than [?&] and [hzs]? There don't seem to be any captures. But, again, something may have been left out for posting purposes. var qs = location.search,
qsMatch = /(\?|&)(?:h|z|s|start(?:view)|return_here)=[^&]+(?:&(?:amp;)?)?/gi;
while (qsMatch.test(qs))
qs = qs.replace(qsMatch, '$1');
var saveName = location.pathname + qs
.replace(/[?&]+$/, ''); string = string.replace(/\b[hzs]=[^&]+&?/gi,"") \b(h|z|s|start(?:view)?|return_here)= and so on, assuming you meant(?:view)?where "view" is optional. If so you could even say s(?:tart(?:view)?)?though I don't suppose the saved picosecond is really worth it.
Is the idea to remove part of the query string, but keep selected bits?
I'm not entirely sure that I understand WHY it workedAnchors do have their uses :) It's like the difference between