Forum Moderators: open
// location.search = "?cs=dude&h=4&z=3&tester=foo&this=that&start=0&lorem=ipsum";
var saveName =
location.search.replace(/([?&])(?:h|z|s|start|startview|return_here)=[^&]+&?/gi, "$1") loc.replace(/([?&])(?:h|z|s|start|startview|return_here)=[^&]*/gi, "$1")
.replace(/&&+/, '&');
I would end up with repeated &&& in the resultYes, it is often easier to adopt a “two steps forward, one step back” approach rather than beat your brains out trying to do it all at once. After the main replacement, do a supplemental
Can the unwanted parameters ever occur as the very first parameter in the string?
Haha, Nick, wouldn't life be so much easier if everyone would just forget about IE?
var exclusions = [ 'h', 'z', 's', 'start', 'startview', 'return_here']
window.location.search
.slice(1,-1) // get rid of the leading ?
.split("&") // split up each param
.filter( // filter function
function(param){ // check each param
return exclusions.indexOf( // against the list of exclusions
param.split('=')[0] // only use the param without the value.
) < 0
})
<?php
$str = '';
foreach ($_GET as $name => $val) {
if ($key != 'h' &&
$key != 'z' &&
$key != 's' &&
$key != 'start' &&
$key != 'startview' &&
$key != 'return_here') {
if ($str != '') $str .= '&';
$str .= $name . '=' . $val;
}
echo <<<EOF
<script>
var saveName = $str;
</script>
EOF;
Do you think that would be better / faster / more reliable / greater longevity than a regex?
greater longevity?
I'm not sure what you mean by that but I think my answer above addresses it.
I don't know the whole story about how, why and where the code is used so it is difficult say.