【问题标题】:discord.js how to slice a linkdiscord.js 如何分割链接
【发布时间】:2020-03-24 05:01:20
【问题描述】:

我正在创建一个蒸汽命令,其中参数是 id 或配置文件链接 我想要做的是得到最后一句话

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

喜欢.steam https://steamcommunity.com/id/ethicalhackeryt/.steam ethicalhackeryt

将 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
    }

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    不确定我是否正确理解了您的问题,但无论您输入的是链接还是名称,此 JS 都会始终为您提供 ID。

    如果这不是您想要的,请再次说明您的问题。

    编辑:将所有内容包装在一个函数中,以便更轻松地显示每种情况。

    console.log(getid("https://steamcommunity.com/id/ethicalhackeryt/"));
    console.log(getid("ethicalhackeryt"));
    
    
    function getid(id) {
      let split = id.split('/');
      split = split.filter(Boolean);
      return split[split.length - 1];
    }

    【讨论】:

      【解决方案2】:

      您可以使用以下正则表达式提取您需要的数据:/^(https:\/\/steamcommunity\.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');
        }
      });

      【讨论】:

        猜你喜欢
        • 2018-12-21
        • 1970-01-01
        • 2020-05-14
        • 1970-01-01
        • 1970-01-01
        • 2021-07-18
        • 2020-11-28
        • 2021-08-13
        • 2021-07-16
        相关资源
        最近更新 更多