简体   繁体   English

Node.js Bot从消息中拉出链接

[英]Node.js Bot pulling links out of messages

I'm making a bot in node.js for Discordapp.com that will pull information from a steam profile's xml. 我正在Discordapp.com的node.js中制作一个机器人,该机器人将从Steam配置文件的xml中提取信息。 It gets the profile links from user messages. 它从用户消息中获取配置文件链接。

I'm having a hard time trying to figure out a way for my bot to pull the link from messages as they're not consistent. 我很难找到一种方法,让我的机器人从消息中拉出链接,因为它们不一致。

Messages could look like: 消息可能看起来像:

http://steamcommunity.com/profiles/76562119803123205611 look at that guys

or 要么

Take a look at this www.steamcommunity.com/id/someusername/

or 要么

look at this steamcommunity.com/profiles/766119803232061 looks cool right?

The only thing consistent in each is " http://steamcommunity.com/ " everything else is different. 唯一一致的是“ http://steamcommunity.com/ ”,其他所有内容都不相同。 Could I receive some help on how I would pull only that entire link from each message and nothing else? 我可以在我如何从每个消息中仅拉出整个链接而没有别的什么方面获得帮助吗?

This is a great place for regular expressions. 这是用于正则表达式的好地方。 Here's one that will match any steamcommunity.com URL with /profiles/ followed by a number and /id/ followed by characters. 这是一个将匹配任何带有/profiles/后跟数字和/id/后跟字符的steamcommunity.com URL的URL。

var test = "www.steamcommunity.com/id/someusername/";
var regexp = new RegExp("(steamcommunity.com)(/id/[a-z]+|(/profiles/(\d)+))", "i");
var out = test.match(regexp)[0];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM