gpt4 book ai didi

javascript - Discord.js 如何切片链接

转载 作者:行者123 更新时间:2023-12-02 22:25:19 25 4
gpt4 key购买 nike

我正在创建一个 steam 命令,其中 args 是 id 或个人资料链接我想做的是得到最后一句话

https://steamcommunity.com/id/ethicalhackeryt/这里我想获得ethicalhackeryt或者如果用户直接输入继续

.steam https://steamcommunity.com/id/ethicalhackeryt/.steamethicalhackeryt

将 args[0] 保存为 ethicalhackeryt

run: async (client, message, args) => {
if(args[0] == `http://steamcommunity.com/id/`) args[0].slice(29); //needed help in this line
const token = steamapi
if(!args[0]) return message.channel.send("Please provide an account name!");
const url ....... rest of code
}

最佳答案

您可以使用以下正则表达式来提取您需要的数据:/^(https:\/\/st​​eamcommunity\.com\/id\/)?([^\s\/]+)\/?$/

基本上,此正则表达式允许 URL 存在(或不存在),后跟任何非空格且非“/”的字符。然后在最后,它允许尾随“/”。

我不知道 Steam 的自定义 URL 中允许使用哪些字符。如果您知道,请将 [^\s\/]+ 替换为与它们匹配的正则表达式。

这还有一个额外的好处,那就是它会拒绝不匹配的值。

const tests = [
'https://steamcommunity.com/id/ethicalhackeryt/',
'https://steamcommunity.com/id/ethicalhackeryt',
'ethicalhackeryt',
'https://google.com/images'
]

tests.forEach(test => {

const id = test.match(/^(https:\/\/steamcommunity\.com\/id\/)?([^\s\/]+)\/?$/);

if (id) {
console.log(test, id[2]);
} else {
console.log(test, 'Not a steam id');
}
});

关于javascript - Discord.js 如何切片链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59090524/

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