取出marketId的参数https://test.wap.banglail.com/?code=0c3n2O0w358dZ03OfK2w3d5gUx2n2O0o&marketId=2/#/market-guide/index
时间: 2023-07-15 21:08:38 浏览: 63
要提取 URL 中的 `marketId` 参数,你可以使用 JavaScript 中的 URLSearchParams 对象。以下是示例代码:
```javascript
const url = new URL('https://test.wap.banglail.com/?code=0c3n2O0w358dZ03OfK2w3d5gUx2n2O0o&marketId=2/#/market-guide/index');
const searchParams = new URLSearchParams(url.search);
const marketId = searchParams.get('marketId');
console.log(marketId); // 输出 "2"
```
在这个例子中,我们首先创建了一个 URL 对象,将 URL 字符串传递给它。然后,我们使用 `URLSearchParams` 来获取 URL 中的查询参数部分。最后,通过调用 `get()` 方法并传递参数名 `'marketId'`,我们可以获取到 `marketId` 的值。在这个例子中,输出结果是 "2"。
注意:如果你的代码运行在旧版的浏览器中,不支持 `URLSearchParams`,你可以使用其他库或自己编写解析 URL 参数的代码。
相关问题
js 一个 正则验证 下面三个链接 https://live.bilibili.com/9906982?a=1 https://live.bilibili.com/9?b=` https://live.bilibili.com/99?c=A
你可以使用正则表达式来验证这三个链接。以下是一个示例的正则表达式,可以用于验证这些链接是否符合要求:
```javascript
const regex = /^https:\/\/live\.bilibili\.com\/\d+\?[a-zA-Z]=\w+$/;
const link1 = "https://live.bilibili.com/9906982?a=1";
const link2 = "https://live.bilibili.com/9?b=`";
const link3 = "https://live.bilibili.com/99?c=A";
console.log(regex.test(link1)); // true
console.log(regex.test(link2)); // false
console.log(regex.test(link3)); // true
```
这个正则表达式的含义是:以`https://live.bilibili.com/`开头,后面跟着一个或多个数字,然后是`?`,再后面是一个字母(大小写不限),最后是一个或多个字母、数字或下划线。
java字符串截取,截取出“https://www.baidu.com/test?a=b”中的test,如何写代码
你好,可以使用以下代码实现字符串截取:
```
String url = "https://www.baidu.com/test?a=b";
String[] parts = url.split("/");
String lastPart = parts[parts.length - 1];
String[] query = lastPart.split("\\?");
String result = query[0];
System.out.println(result); // 输出 test
```
首先,使用`split()`方法将字符串按照`/`分割成一个字符串数组。然后,取数组中的最后一个元素(即`test?a=b`),再次按照`?`分割成一个新的数组。最后,取该数组的第一个元素(即`test`)作为要截取的字符串返回。