jQuery 使用正则表达式从 url 中删除查询字符串参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18366147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:26:58  来源:igfitidea点击:

Remove querystring parameters from url with regex

javascriptjqueryregex

提问by cpeele00

I'm pretty new to regex and need to remove some content from our url

我对正则表达式很陌生,需要从我们的网址中删除一些内容

 http://mysite.blah/problem/smtp/smtp-open-relay?page=prob_detail&showlogin=1&action=smtp:134.184.90.18

I need to remove everything from the "?" and on, leaving me just:

我需要从“?”中删除所有内容 然后,让我只剩下:

http://mysite.blah/problem/smtp/smtp-open-relay

Here is our current regex expression we are using to grab the route data. For example I can grab "smtp" and "smtp-open-relay" (which we need). However sometimes our url changes depending on where the user is coming from thereby appending the querystring parameters which is causing our current regex expression to blow up.

这是我们当前用于获取路线数据的正则表达式。例如,我可以获取“smtp”和“smtp-open-relay”(我们需要)。然而,有时我们的 url 会根据用户的来源而变化,从而附加查询字符串参数,这会导致我们当前的正则表达式崩溃。

// Retrieve the route data from the route
var routeData = /([0-9a-zA-Z_.-]+)\/([0-9a-zA-Z_.-]+)$/g.exec(route);

I need it to ignore stuff from the "?" on.

我需要它来忽略“?”中的内容 在。

回答by George

A regular expression is probably more than you need.

正则表达式可能超出您的需要。

You could do the following to remove the ?and everything (query string + hash) after it:

您可以执行以下操作来删除它之后的?所有内容(查询字符串 + 哈希):

var routeData = route.split("?")[0];

If you truly wanted to strip only the query string, you could preserve the hash by reconstructing the URL from the window.locationobject:

如果您真的只想删除查询字符串,则可以通过从window.location对象重建 URL 来保留哈希:

var routeData = window.location.origin + window.location.pathname + window.location.hash;

If you want the query string, you can read it with window.location.search.

如果需要查询字符串,可以使用window.location.search.

回答by john Smith

i just used this one

我刚用过这个

    var routeData= route.substring(0, route.indexOf('?'));

回答by sidanmor

Use this function:

使用这个功能:

var getCleanUrl = function(url) {
  return url.replace(/#.*$/, '').replace(/\?.*$/, '');
};

// get rid of hash and params
console.log(getCleanUrl('https://sidanmor.com/?firstname=idan&lastname=mor'));

回答by Pratap Singh

Following is the cleaner way to remove a given parameter say: prop1 form querystring of url. Querystring can be found in url by accessing

以下是删除给定参数的更简洁的方法说:prop1 表单 url 的查询字符串。查询字符串可以通过访问在 url 中找到

window.location.search

窗口位置搜索

Here you apply regular expression for prop1:

在这里为 prop1 应用正则表达式:

var queryStringWithoutProp1=window.location.search.replace(/(&?prop1=)(.[^&]*)/,"");

var queryStringWithoutProp1=window.location.search.replace(/(&?prop1=)(.[^&]*)/,"");

queryStringWithoutProp1 must return querystring without prop1=value parameter-value combination from querystring

queryStringWithoutProp1 必须从 querystring 返回没有 prop1=value 参数-值组合的 querystring

Note:'&?' ensures whether prop1 appears as first parameter or any subsequent one.

注意:'&?' 确保 prop1 是作为第一个参数出现还是作为任何后续参数出现。

回答by Trevor Dixon

If you're doing this in-browser, let the browser do the parsing:

如果您在浏览器中执行此操作,请让浏览器进行解析:

location.origin + location.pathname

Or for arbitrary URLs:

或者对于任意 URL:

function withoutQS(_url) {
    var url = document.createElement('a');
    url.href = _url;
    return url.origin + url.pathname;
}