gpt4 book ai didi

javascript - 从 url 查询字符串中获取特定值

转载 作者:搜寻专家 更新时间:2023-11-01 04:44:47 26 4
gpt4 key购买 nike

我有以下 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);

我想提取 ID“B0077RQGX4”并将其保存到我稍后修改的变量中。我该怎么做,我会在 JavaScript 中使用哪些函数

最佳答案

我想到了这个:

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 = "";
}

适用于:

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'

关于javascript - 从 url 查询字符串中获取特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10126712/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com