javascript 从 url 查询字符串中获取特定值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10126712/
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
Getting specific value from url query string
提问by W H Help
I have the following URL, and I want to get the "id" value from it using JavaScript.
我有以下 URL,我想使用 JavaScript 从中获取“id”值。
https://www.blabla.com/ebookedit?id=B0077RQGX4&commit=Go
I started with this code:
我从这个代码开始:
var url = document.URL;
var id_check = /^\id...\E; // NOT SURE HERE
var final_id = new RegExp(id_check,url);
I want to extract the id "B0077RQGX4" and save it into a variable that I would later modify. How would I do it and which functionswould I use in JavaScript?
我想提取 id “ B0077RQGX4”并将其保存到我稍后要修改的变量中。我将如何做以及我将在 JavaScript 中使用哪些函数?
回答by Adri V.
I came up with this:
我想出了这个:
var final_id;
var url = document.URL;
var id_check = /[?&]id=([^&]+)/i;
var match = id_check.exec(url);
if (match != null) {
final_id = match[1];
} else {
final_id = "";
}
Works for:
效劳于:
https://www.blabla.com/ebookedit?id=B0077RQGX4&commit=Go
final_id = 'B0077RQGX4'
https://www.blabla.com/ebookedit?SomethingElse=Something&id=B0077RQGX4&commit=Go
final_id = 'B0077RQGX4'
https://www.blabla.com/ebookedit?commit=go&id=B0077RQGX4
final_id = 'B0077RQGX4'
https://www.blabla.com/ebookedit?commit=Go
final_id = ''
https://www.blabla.com/ebookedit?id=1234&Something=1&id=B0077RQGX4&commit=Go
final_id = '1234'
回答by TLS
While you can do this with Regex, it's probably going to be easier and/or more consistent if you use a non-Regex approach. This is the case because the query string could have a wide variety of layouts (with the id
value first, last, or in the middle).
虽然您可以使用 Regex 做到这一点,但如果您使用非 Regex 方法,它可能会更容易和/或更一致。这是因为查询字符串可以有多种布局(id
值 first、last 或 in middle)。
EDIT:@AdrianaVillafa?e proves that this can be done easily with a Regex! I'm going to leave this JS-only method here because it does work.
编辑:@AdrianaVillafa?e 证明这可以通过正则表达式轻松完成!我将把这个 JS-only 方法留在这里,因为它确实有效。
I like to use this JavaScript method to parse query string from a URL and get the first value that matches the desired name. In your case, "id" would be the name
parameter.
我喜欢使用这种 JavaScript 方法来解析 URL 中的查询字符串并获取与所需名称匹配的第一个值。在您的情况下,“id”将是name
参数。
// parses the query string provided and returns the value
function GetQueryVariable(query, name) {
if (query.indexOf("?") == 0) { query = query.substr(1); }
var pairs = query.split("&");
for (var i = 0; i < pairs.length; i++) {
var pair = pairs[i].split("=");
if (pair[0] == name) {
return pair[1];
}
}
return "";
}
To use this method, you would pass in the query string portion of the URL and the name of the value that you want to get. If you want to parse the URL of the current request, you could do this:
要使用此方法,您需要传入 URL 的查询字符串部分和要获取的值的名称。如果你想解析当前请求的 URL,你可以这样做:
var value = GetQueryVariable(location.search, "id");
Trying to do this in a Regex will most likely be inconsistent at best when trying to handle the possible variations of the query string layout.
在尝试处理查询字符串布局的可能变化时,尝试在 Regex 中执行此操作最有可能是不一致的。
回答by Manu Mathew
Just try this.
试试这个。
function getUrlVars()
{
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value)
{
vars[key] = value;
});
return vars;
}
function urlval()
{
var x=getUrlVars()["id"];
alert (x);
}
Now x will give value in the id as B0077RQGX4
现在 x 将在 id 中给出值 B0077RQGX4
回答by qw3n
Well it isn't regex but you could just do
嗯,这不是正则表达式,但你可以这样做
id=url.split('id=')[1].split('&')[0];