Javascript discord.js如何分割链接
我正在创建一个steam命令,其中args是id或profile链接 我想做的是得到最后的决定 前Javascript discord.js如何分割链接,javascript,node.js,discord.js,Javascript,Node.js,Discord.js,我正在创建一个steam命令,其中args是id或profile链接 我想做的是得到最后的决定 前 https://steamcommunity.com/id/ethicalhackeryt/这里我想得到ethicalhackeryt或者如果用户直接输入继续 比如。蒸汽https://steamcommunity.com/id/ethicalhackeryt/ 或.steam ethicalhackeryt 将参数[0]另存为ethicalhackeryt run: async (client,
https://steamcommunity.com/id/ethicalhackeryt/
这里我想得到ethicalhackeryt
或者如果用户直接输入继续
比如。蒸汽https://steamcommunity.com/id/ethicalhackeryt/
或.steam ethicalhackeryt
将参数[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
}
不确定我是否正确理解了您的问题,但无论您输入的是链接还是名称,此JS都会始终为您提供ID 如果这不是您想要的,请再次指定您的问题 编辑:将所有内容包装在一个函数中,以便更容易地显示每种情况
console.log(getid(“https://steamcommunity.com/id/ethicalhackeryt/"));
日志(getid(“ethicalhackeryt”);
函数getid(id){
设split=id.split('/');
split=split.filter(布尔值);
返回分割[split.length-1];
}
您可以使用以下正则表达式提取所需的数据:/^(https:\/\/steamcommunity\.com\/id\/)?([^\s\/]+)\/?$/
基本上,这个正则表达式允许URL存在(或不存在),后跟任何非空格和“/”字符。然后在末尾,它允许一个尾随“/”
我不知道steam在自定义URL中允许哪些字符。如果您知道,请将[^\s\/]+
替换为与之匹配的正则表达式
这样做的另一个好处是,它将拒绝不匹配的值
const测试=[
'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\/]+)\/?$/);
如果(id){
控制台日志(测试,id[2]);
}否则{
日志(测试“不是蒸汽id”);
}
});代码>