Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

i am creating a steam command where the args are either the id or the profile link what i want to do is get the last word(我正在创建一个Steam命令,其中args是ID或配置文件链接,我想要做的就是得到最后一个单词)

ex https://steamcommunity.com/id/ethicalhackeryt/ here i want to get ethicalhackeryt or if user inputs that directly the continue(前https://steamcommunity.com/id/ethicalhackeryt/在这里,我想获得ethicalhackeryt或者如果用户直接输入继续) like .steam https://steamcommunity.com/id/ethicalhackeryt/ or .steam ethicalhackeryt(例如.steam https://steamcommunity.com/id/ethicalhackeryt/.steam ethicalhackeryt) save args[0] as 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 }   ask by ethyt translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
371 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can use the following regex to pull out the data you need: /^(https:\/\/steamcommunity\.com\/id\/)?([^\s\/]+)\/?$/(您可以使用以下正则表达式提取所需的数据:/ /^(https:\/\/steamcommunity\.com\/id\/)?([^\s\/]+)\/?$/ : /^(https:\/\/steamcommunity\.com\/id\/)?([^\s\/]+)\/?$/)

Basically, this regex allows for the URL to be there (or not), followed by any characters that are not whitespace and not "/".(基本上,此正则表达式允许URL存在(或不存在),后跟不是空格也不是“ /”的任何字符。) Then at the end, it allows for a trailing "/".(然后最后,它允许在末尾加上“ /”。) I don't know what characters steam allows in their custom URLs.(我不知道Steam允许在其自定义URL中使用哪些字符。) If you know, replace [^\s\/]+ with a regex that matches them.(如果您知道,请用与它们匹配的正则表达式替换[^\s\/]+ 。) This has the added benefit that it will reject values that do not match.(这具有额外的好处,它将拒绝不匹配的值。) 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'); } });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...