如何从url查询中删除参数?

时间:2022-08-24 13:04:46

I am trying to remove some parameters from the url query string.

我试图从url查询字符串中删除一些参数。

I have an array of parameters to be removed:

我有一个要删除的参数数组:

var queryParamsToRemove = ['destination','formName'];

What I was trying to do, is search for each parameter in the url, and remove it using a RegExp. Something like this:

我试图做的是搜索网址中的每个参数,并使用RegExp将其删除。像这样的东西:

for (param in queryParamsToRemove){
     patt = new RegExp(param,'g');
     queryString = queryString.replace('&' + patt + '=' + /*?=&/g,'');
}

But this doesn't work, because of syntax problems, I guess.

但是,由于语法问题,我认为这不起作用。

3 个解决方案

#1


1  

Here is a function that I often use in my projects, it allows you to fetch all GET parameters in an associative array:

这是我经常在我的项目中使用的函数,它允许您获取关联数组中的所有GET参数:

function urlGet()
{   
    var t = location.search.substring(1).split('&');
    var f = new Object();
    for (var i = 0; i < t.length; i++)
    {
        var x = t[i].split('=');
        f[x[0]] = x[1];
    }
    return f;
}

You can use this function to get all parameters and then, remove from array the parameters you don't want with delete method.

您可以使用此函数获取所有参数,然后使用delete方法从数组中删除不需要的参数。

var parameters = urlGet();
delete parameters.destination;
delete parameters.formName;

You now just have to build a new query string.

您现在只需要构建一个新的查询字符串。

var query = '?';

for (var prop in parameters) 
{
    query += prop + '=' + parameters[prop] + '&';
};

query = query.substr(0, query.length - 1);

Now, add it to your base url and you're done!

现在,将它添加到您的基本网址,您就完成了!

#2


0  

To answer your real real question:

要回答你真正的真实问题:

for (param in queryParamsToRemove){
    queryString = queryString.replace(new RegExp('[&\?]' + queryParamsToRemove[param] + '=.*(&?)', 'g'), '\1');
}

That should work.

这应该工作。

Otherwise you could use a function like this one: The $.param( ) inverse function in JavaScript / jQuery (Simply remove jQuery from function name and you can use it without any library).

否则你可以使用像这样的函数:JavaScript / jQuery中的$ .param()反函数(简单地从函数名中删除jQuery,你可以在没有任何库的情况下使用它)。

Something like this:

像这样的东西:

unparam = function (value) {
    var
    // Object that holds names => values.
    params = [],
    // Get query string pieces (separated by &)
    pieces = value.split('&'),
    // Temporary variables used in loop.
    pair, i, l;

    // Loop through query string pieces and assign params.
    for (i = 0, l = pieces.length; i < l; i++) {
        pair = pieces[i].split('=', 2);
        // Repeated parameters with the same name are overwritten. Parameters
        // with no value get set to boolean true.
        params[decodeURIComponent(pair[0])] = (pair.length == 2 ?
            decodeURIComponent(pair[1].replace(/\+/g, ' ')) : true);
    }

    return params;
};

var query = unparam(querystr);
for(var i in queryParamsToRemove) {
    delete query[queryParamsToRemove[i]];
}

Turning the query string into an array, and from there you can remove your values and convert it back to a query string. If you use jQuery you can use jQuery.param()

将查询字符串转换为数组,然后从中删除您的值并将其转换回查询字符串。如果你使用jQuery你可以使用jQuery.param()

Otherwise it shouldn't be too hard to make your own function.

否则,制作自己的功能应该不会太难。

To now convert the query array into a string we could do:

现在将查询数组转换为我们可以执行的字符串:

reparam = function (queryArray) {
    var res = [];

    for(var i in queryArray) {
        res.push(i + "=" + queryArray[i]);   
    }

    return res.join('&');
}

var queryString = reparam(query);

#3


0  

Ok, found something that works!

好的,找到了有效的东西!

for (param in queryParamsToRemove) {
    queryString = queryString.replace(new RegExp('&(' + queryParamsToRemove[param] + '=)+((\w)*(.)*)*&', 'g'), '&');
}

#1


1  

Here is a function that I often use in my projects, it allows you to fetch all GET parameters in an associative array:

这是我经常在我的项目中使用的函数,它允许您获取关联数组中的所有GET参数:

function urlGet()
{   
    var t = location.search.substring(1).split('&');
    var f = new Object();
    for (var i = 0; i < t.length; i++)
    {
        var x = t[i].split('=');
        f[x[0]] = x[1];
    }
    return f;
}

You can use this function to get all parameters and then, remove from array the parameters you don't want with delete method.

您可以使用此函数获取所有参数,然后使用delete方法从数组中删除不需要的参数。

var parameters = urlGet();
delete parameters.destination;
delete parameters.formName;

You now just have to build a new query string.

您现在只需要构建一个新的查询字符串。

var query = '?';

for (var prop in parameters) 
{
    query += prop + '=' + parameters[prop] + '&';
};

query = query.substr(0, query.length - 1);

Now, add it to your base url and you're done!

现在,将它添加到您的基本网址,您就完成了!

#2


0  

To answer your real real question:

要回答你真正的真实问题:

for (param in queryParamsToRemove){
    queryString = queryString.replace(new RegExp('[&\?]' + queryParamsToRemove[param] + '=.*(&?)', 'g'), '\1');
}

That should work.

这应该工作。

Otherwise you could use a function like this one: The $.param( ) inverse function in JavaScript / jQuery (Simply remove jQuery from function name and you can use it without any library).

否则你可以使用像这样的函数:JavaScript / jQuery中的$ .param()反函数(简单地从函数名中删除jQuery,你可以在没有任何库的情况下使用它)。

Something like this:

像这样的东西:

unparam = function (value) {
    var
    // Object that holds names => values.
    params = [],
    // Get query string pieces (separated by &)
    pieces = value.split('&'),
    // Temporary variables used in loop.
    pair, i, l;

    // Loop through query string pieces and assign params.
    for (i = 0, l = pieces.length; i < l; i++) {
        pair = pieces[i].split('=', 2);
        // Repeated parameters with the same name are overwritten. Parameters
        // with no value get set to boolean true.
        params[decodeURIComponent(pair[0])] = (pair.length == 2 ?
            decodeURIComponent(pair[1].replace(/\+/g, ' ')) : true);
    }

    return params;
};

var query = unparam(querystr);
for(var i in queryParamsToRemove) {
    delete query[queryParamsToRemove[i]];
}

Turning the query string into an array, and from there you can remove your values and convert it back to a query string. If you use jQuery you can use jQuery.param()

将查询字符串转换为数组,然后从中删除您的值并将其转换回查询字符串。如果你使用jQuery你可以使用jQuery.param()

Otherwise it shouldn't be too hard to make your own function.

否则,制作自己的功能应该不会太难。

To now convert the query array into a string we could do:

现在将查询数组转换为我们可以执行的字符串:

reparam = function (queryArray) {
    var res = [];

    for(var i in queryArray) {
        res.push(i + "=" + queryArray[i]);   
    }

    return res.join('&');
}

var queryString = reparam(query);

#3


0  

Ok, found something that works!

好的,找到了有效的东西!

for (param in queryParamsToRemove) {
    queryString = queryString.replace(new RegExp('&(' + queryParamsToRemove[param] + '=)+((\w)*(.)*)*&', 'g'), '&');
}